Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize python lists?

I have different threads and after processing they put data in a common list. Is there anything built in python for a list or a numpy array to be accessed by only a single thread. Secondly, if it is not what is an elegant way of doing it?

like image 503
Shan Avatar asked Jun 19 '12 07:06

Shan


2 Answers

According to Thread synchronisation mechanisms in Python, reading a single item from a list and modifying a list in place are guaranteed to be atomic. If this is right (although it seems to be partially contradicted by the very existence of the Queue module), then if your code is all of the form:

try:
   val = mylist.pop()
except IndexError:
   # wait for a while or exit
else:
   # process val

And everything put into mylist is done by .append(), then your code is already threadsafe. If you don't trust that one document on that score, use a queue.queue, which does all synchronisation for you, and has a better API than list for concurrent programs - particularly, it gives you the option of blocking indefinitely, or for a timeout, waiting for .pop() to work if you don't have anything else the thread could be getting on with in the mean time.

For numpy arrays, and in general any case where you need more than a producer/consumer queue, use a Lock or RLock from threading - these implement the context manager protocol, so using them is quite simple:

with mylock:
    # Process as necessarry

And python will guarantee that the lock gets released once you fall off the end of the with block - including in tricky cases like if something you do raises an exception.

Finally, consider whether multiprocessing is a better fit for your application than threading - threads in Python aren't guaranteed to actually run concurrently, and in CPython only can if the drop to C-level code. multiprocessing gets around that issue, but may have some extra overhead - if you haven't already, you should read the docs to determine which one suits your needs better.

like image 101
lvc Avatar answered Sep 20 '22 10:09

lvc


threading provides Lock objects if you need to protect an entire critical section, or the Queue module provides a queue that is threadsafe.

like image 40
Amber Avatar answered Sep 20 '22 10:09

Amber