Are generator functions thread-safe?

David Eppstein eppstein at
Mon Sep 16 17:58:17 CEST 2002

In article <am4t4l$b33$1 at>, aahz at (Aahz) 

> >I know that a call to a generator function actually returns an independent
> >iterator object (mea culpa if I'm wrong on this), but are there any hidden
> >gotchas that would cause the use of the same generator function from
> >multiple threads, to result in a sharing (especially local storage) or
> >memory collision issue?
> The same gotchas for any other function: you're screwed if you share
> mutable objects without locks.

Also, if a call to the generator object is running from one thread, you 
will get an exception if you attempt to simultaneously call it (e.g. 
from another thread).  I don't know how thread-safe is the code that 
prevents the generator from being called twice simultaneously, though.

David Eppstein       UC Irvine Dept. of Information & Computer Science
eppstein at

More information about the Python-list mailing list