Reputation: 647
I have three threads in Python that are sharing ASCII data. The first thread reads serial data from a serial port at an extremely fast rate. Then, I parse and pass that data to a socket where it's written. Additionally, some of the data from the serial data is written to an XML file. I'm aware of the global interpreter lock and its limitations. I need to do all of these operations within 1/10 of a second. I'm seeing a large amount of CPU usage and a delay by utilizing global values that I share between the threads. I don't think I can use events in Python as I would with other languishes.
How do I share data between these threads in a synchronous matter without the delay and CPU usage in Python?
Upvotes: 3
Views: 3870
Reputation: 144
I think that the best way to implement this is by using python Queue module: https://docs.python.org/2/library/queue.html
The queues are basically lists but they are thread-safe and the 'get' function is efficiently blocking the thread until some data is enqueued in it. It should be more efficient for the CPU.
Upvotes: 4