Reputation: 41
I am writing code which should have the following structure:
Could someone give me some code/pseudo code on how to do something like this. Mainly on what kind of buffer that I should use & how the threads communicate such that all data is collected and processed correctly.
I have read up on the "Queue" and "threading" class in python. However, I am still not sure on how to do this properly. I have tried multiple easy examples with the threading.Event() object - however results are inconsistent.
Thanks alot!
Upvotes: 0
Views: 138
Reputation: 2111
If your worker can see the collectors being done, then it's as simple as checking while the threads are not done OR the queue is not empty.
If your worker cannot see the collectors state or even how many there are, you can have the collectors communicate that state in the queue itself, along the data.
Imagine the data object has an origin field, and a boolean field to mark as open or closed. For example, collector 1 would send something like:
data-1-opened, data-1-opened ... data-1-closed.
The worker only has to maintain a set of collector ids, adding new entries as they appear, and removing them when closed. So the exit condition is that the set is empty AND the data queue is empty.
Upvotes: 1