Communicate with the labjack "the parallel way" | LabJack
 

Communicate with the labjack "the parallel way"

3 posts / 0 new
Last post
francoisbiar
francoisbiar's picture
Communicate with the labjack "the parallel way"

Hello,

I try to communicate with a LabJack T7 in python, using the multiprocessing library. In this example, I have two different processes : one to read an analog input, and one to write an analog output. I synchronized both processes (with one pipe) to avoid concurrency on labjack access, but the script still doesn't work : I can't do a writing AND a reading in this mode, although the operations are exactly the same as if I used the writing and reading commands sequentially.

Please find attached the script I use to describe my problem. I wrote 3 methods : one to read an analog input, one to write an analog output, and one to do both sequentially (the "classic" way). A pipe permits to synchronize both methods. The boolean 'parallel' at the beginning of the file permits to execute either the serial or the parallel way.

Am I missing something, or is it even possible to do this.

Thanks for your reply

Francois

File Attachment: 
LabJack Support
labjack support's picture
Only one process can have a

Only one process can have a device opened/claimed at a time over USB, and the device handle cannot be shared over multiple processes. Your code will currently try to open the T7 3 times, once in your main process and the two that it spawns, and doesn't close the T7. This is causing LJME_DEVICE_CURRENTLY_CLAIMED_BY_ANOTHER_PROCESS errors.

Here are some solutions to look into:

1. If Python threading is adequate, use threads instead. Your one process can open the T7 and the various threads can share the device handle.

2. Limit T7 communications to one process, like a server. Other processes can communicate with the T7 controlling process to perform operations.

3. Use TCP communications instead. Each process will open the T7 over TCP and use its own handle for communications. Note that the T7 has a socket limit of two, so only two processes can open and access a T7 over TCP at a time.

4. In each process, over USB open, perform operations and close the T7. Make sure T7 operations don't overlap each in processes or you'll get LJME_DEVICE_CURRENTLY_CLAIMED_BY_ANOTHER_PROCESS errors when opening the T7. This may cause the same issue you were encountering here though:

https://labjack.com/forums/linux/bug-frequent-ljmereconnectfailed-python...

LabJack Support
labjack support's picture
To update my "once in your

To update my "once in your main process and the two that it spawns, and doesn't close the T7" statement, when using your parallel code a KeyBoardInterrupt in your read process (2) will try to close the handle, though your code doesn't reach that point since there is a unhandled openS exception in that process.