Suggested generator to add to threading module.
Jeff Epler
jepler at unpythonic.net
Fri Jan 16 15:02:52 EST 2004
On Fri, Jan 16, 2004 at 08:42:36PM +0100, Ype Kingma wrote:
> > Found myself needing serialised access to a shared generator from
> > multiple threads. Came up with the following
> >
> > def serialise(gen):
> > lock = threading.Lock()
> > while 1:
> > lock.acquire()
> > try:
> > next = gen.next()
> > finally:
> > lock.release()
> > yield next
>
> Is there any reason why the lock is not shared among threads?
> >From the looks of this, it doesn't synchronize anything
> between different threads. Am I missing something?
Yes, I think so. You'd use the same "serialise" generator object in
multiple threads, like this:
p = seralise(producer_generator())
threads = [thread.start_new(worker_thread, (p,))
for t in range(num_workers)]
Jeff
More information about the Python-list
mailing list