thread-safety

Is the += operator thread-safe in Python?

Is the += operator thread-safe in Python? Question: I want to create a non-thread-safe chunk of code for experimentation, and those are the functions that 2 threads are going to call. c = 0 def increment(): c += 1 def decrement(): c -= 1 Is this code thread safe? If not, may I understand why …

Total answers: 8

Are Generators Threadsafe?

Are Generators Threadsafe? Question: I have a multithreaded program where I create a generator function and then pass it to new threads. I want it to be shared/global in nature so each thread can get the next value from the generator. Is it safe to use a generator like this, or will I run into …

Total answers: 6

Queue.Queue vs. collections.deque

Queue.Queue vs. collections.deque Question: I need a queue which multiple threads can put stuff into, and multiple threads may read from. Python has at least two queue classes, Queue.Queue and collections.deque, with the former seemingly using the latter internally. Both claim to be thread-safe in the documentation. However, the Queue docs also state: collections.deque is …

Total answers: 7