Are generator functions thread-safe?

David Eppstein eppstein at ics.uci.edu
Mon Sep 16 11:58:17 EDT 2002


In article <am4t4l$b33$1 at panix1.panix.com>, aahz at pythoncraft.com (Aahz) 
wrote:

> >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 ics.uci.edu http://www.ics.uci.edu/~eppstein/



More information about the Python-list mailing list