I am working on a python project that is polling for data on a COM port and also polling for user input. As of now, the program is working flawlessly but seems to be inefficient. I have the serial port polling occurring in a while loop running in a separate thread and sticking data into a Queue. The user input polling is also occurring in a while loop running in a separate thread sticking input into a Queue. Unfortunately I have too much code and posting it would take away from the point of the question.
So is there a more efficient way to poll a serial or raw_input() without sticking them in an infinite loop and running them in their own thread?
I have been doing a lot of research on this topic and keep coming across the "separate thread and Queue" paradigm. However, when I run this program I am using nearly 30% of my CPU resources on a quad-core i7. There has to be a better way.
I have worked with ISR's in C and was hoping there is something similar to interrupts that I could be using. My recent research has uncovered a lot of "Event" libraries with callbacks but I can't seems to wrap my head around how they would fit in my situation. I am developing on a Windows 7 (64-bit) machine but will be moving the finished product to a RPi when I am finished. I'm not looking for code, I just need to be pointed in the right direction. Thank you for any info.
get_nowaitor some other non-blockinggetcall, that's probably what's using the CPU cycles. Could you refactor the code so that one queue is shared with all three threads? You could change the payload of the messages you put into the queue so that you can identify which thread it's from (COM or raw_input), and then just use a blockingqueue.getin the main thread. - danoget_nowaitin my main thread for polling both queues. I can easily refactor things so they share a queue and I will give the payloads an identifier as you have suggested. So by using the blocking version (get) the queue will not be polled until one of my separate threads has added something to it? Or I am completely missing the idea of blocking vs. non-blocking when polling a queue? - Steve