openpaperwork / pyinsane

Python library to access and use image scanners (Linux/Windows/etc) (Sane/WIA) -- Moved to Gnome's Gitlab
https://gitlab.gnome.org/World/OpenPaperwork/pyinsane
63 stars 24 forks source link

Multi Threading scanning #32

Open astadtla opened 7 years ago

astadtla commented 7 years ago

I have a use case where I need to scan 2 different scanners at the same time. when I attempt to multi thread pyinsane2 I get the following TestScanMultiple.txt

error. attached is the python script that generated this error. Any help would be greatly appreciated.

===== RESTART: /home/pi/Documents/LinearBookScanner/TestScanMultiple.py =====

Device Id: 0Device Id: 1

init in thread: 1init in thread: 2

Devices Recieved in thread: 2 Exiting in thread: 2 Exception in thread Thread-1: Traceback (most recent call last): File "/home/pi/Documents/LinearBookScanner/TestScanMultiple.py", line 12, in Scan devices = pyinsane2.get_devices() File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 268, in get_devices for x in remote_do('get_devices', local_only) File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 69, in remote_do result = os.read(fifo_s2c, length) OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/pi/Documents/LinearBookScanner/TestScanMultiple.py", line 16, in Scan pyinsane2.exit() File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 120, in exit remote_do('exit') File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 56, in remote_do os.write(fifo_c2s, length) OSError: [Errno 9] Bad file descriptor

jflesch commented 7 years ago

Please call pyinsane2.init() only once from a single thread (for instance in your function Program()). (same goes for pyinsane2.exit()).

astadtla commented 7 years ago

TestScanMultiple.txt I moved all of the references to pyinsane2 outside of the threads but when i get to the section to start the scan in the any one of the threads: try: while True: scan_session.scan.read() except EOFError: pass i get another error: ===== RESTART: /home/pi/Documents/LinearBookScanner/TestScanMultiple.py ===== Device Id: 0Device Id: 1

Exiting in thread: 1Exiting in thread: 2

Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, *self._kwargs) File "/home/pi/Documents/LinearBookScanner/TestScanMultiple.py", line 18, in Scan scan_session.scan.read() File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 173, in read return remote_do('scan_read', self._scanner_name) File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 69, in remote_do result = os.read(fifo_s2c, length) MemoryError Exception in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(self._args, **self._kwargs) File "/home/pi/Documents/LinearBookScanner/TestScanMultiple.py", line 18, in Scan scan_session.scan.read() File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 173, in read return remote_do('scan_read', self._scanner_name) File "/usr/local/lib/python3.5/dist-packages/pyinsane2/sane/abstract_proc.py", line 70, in remote_do assert(len(result) == length) AssertionError

jflesch commented 7 years ago

MemoryError

How much free memory does your device has ? It seems it's running out of memory.

Unfortunately, Pyinsane uses quite a lot of memory to store the chunk of the images. In another bug report, someone reported having issues scanning at 600dpi with only 1GB of ram (raspberry pi).

astadtla commented 7 years ago

I am using a pi, but I have a swap file that adds a GB so 2 in total. I am only scanning at 150 dpi so it should not be to much of a hog at that level. Was the error replicated on your machine with the script I attached in the previous comment?