How to sync python lists?

I have different streams, and after processing they put the data in a general list. Is there anything built in python for a list or a numpy array that can only be accessed by one thread. Secondly, if this is not the elegant way to do this?

+5
source share
3 answers

threadingprovides objects Lockif you need to protect the entire critical section or Queueprovide a queue that is thread safe.

+4
source

Python, . (, , Queue), :

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

, mylist, .append(), . , queue.queue, API list - , -, , .pop() , , .

numpy , , /, Lock RLock threading - , :

with mylock:
    # Process as necessarry

python , , with, , , - , .

, , multiprocessing, threading - Python , CPython C- . multiprocessing , - , , , .

+4

What about the Queue standard library ?

+1
source

All Articles