generators shared among threads
Just
just at xs4all.nl
Wed Mar 8 06:50:15 EST 2006
In article <440eb5ad_2 at newspeer2.tds.net>,
Kent Johnson <kent at kentsjohnson.com> wrote:
> Paul Rubin wrote:
> > Hmm (untested, like above):
> >
> > class Synchronized:
> > def __init__(self, generator):
> > self.gen = generator
> > self.lock = threading.Lock()
> > def next(self):
> > self.lock.acquire()
> > try:
> > yield self.gen.next()
> > finally:
> > self.lock.release()
> >
> > synchronized_counter = Synchronized(itertools.count())
> >
> > That isn't a general solution but can be convenient (if I didn't mess
> > it up). Maybe there's a more general recipe somewhere.
>
> This code is not allowed in Python 2.4. From PEP 255:
[ snip ]
The code also doesn't make sense: .next() should *return* a value, not
yield one. Substituting "return" for "yield" might just work for the
code above.
Just
More information about the Python-list
mailing list