I am trying to gather data from a camera and a sensor using one loop for each and python Threading. The problem is that when I run each loop independently they work, but at the same time using threading only one of the two works and if one crashes, then the other starts working. I am using the powermode MODE30ALL and the exact same code works fine on a classic computer.
Could you share a sample code with us to reproduce this?
We can use multi-threading in C++ well but don’t have too much experience on python threading.
The python threading module does not release the GIL (global interpreter lock) so even if multiple OS threads are launched, only one is scheduled to execute at a time (the idea is to context shift with sleep(), spending most of your time waiting on external things). To actually do more than one thing at once in parallel, you must use the multiprocessing module which launches multiple instances of the python interpreter or write a c extension (or use Cython to do so) which releases the gil, does stuff in parallel, and re-acquires it (not doing so segfaults). Rule of thumb: the threading module (or async) is for stuff that’s IO bound while multiprocessing is for stuff that’s CPU bound.
No guarantees it will fix it, but I think the problem is there is no sleep() in your second loop to context switch. I am not sure if cv2.waitkey will do the trick.
Thank you so much for your response mdegans. Using the multiprocessing module did the trick but I think I’ll switch my code to c++ so I won’t be in trouble again.