Are Generators Threadsafe?

Corey Goldberg picture Corey Goldberg · Jul 15, 2009 · Viewed 15.1k times · Source

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 problems/conditions accessing the shared generator from multiple threads?

If not, is there a better way to approach the problem? I need something that will cycle through a list and produce the next value for whichever thread calls it.

Answer

Martin v. Löwis picture Martin v. Löwis · Jul 15, 2009

It's not thread-safe; simultaneous calls may interleave, and mess with the local variables.

The common approach is to use the master-slave pattern (now called farmer-worker pattern in PC). Make a third thread which generates data, and add a Queue between the master and the slaves, where slaves will read from the queue, and the master will write to it. The standard queue module provides the necessary thread safety and arranges to block the master until the slaves are ready to read more data.