[Python-ideas] Thread-safe generators

Serhiy Storchaka storchaka at gmail.com
Sat Apr 15 00:55:02 EDT 2017


On 15.04.17 01:42, Greg Ewing wrote:
> Serhiy Storchaka wrote:
>> but should not affect performance since locking is used only when you
>> faced with a generator running in other thread.
>
> I don't think that's true, because the first thread to use a
> generator has to lock it as well. And even if there is only
> one thread in existence when __next__ is called, another one
> could be created before it finishes and try to use the
> generator.

The first thread just sets the running flag (as in current code). Due to 
GIL this doesn't need additional synchronization. Other threads check 
this flag and sleep rather than raising an exception. After finishing 
the generator the first thread checks if there are other threads 
awaiting and awake a one of them.




More information about the Python-ideas mailing list