generators shared among threads

Bryan Olson fakeaddress at nowhere.org
Wed Mar 8 17:41:00 CET 2006


jess.austin at gmail.com wrote:
> Paul wrote:
> 
>>   def f():
>>       lock = threading.Lock()
>>       i = 0
>>       while True:
>>           lock.acquire()
>>           yield i
>>           i += 1
>>           lock.release()
>>
>>but it's easy to make mistakes when implementing things like that
>>(I'm not even totally confident that the above is correct).
> 
> 
> The main problem with this is that the yield leaves the lock locked.
> If any other thread wants to read the generator it will block.

I don't think so. The second thread will start right after the
yeild, and release the lock before acquiring it.

Here's a demo:


import threading
import time

def start_daemon(closure):
       t = threading.Thread(target=closure)
       t.setDaemon(True)
       t.start()

def f():
     lock = threading.Lock()
     i = 0
     while True:
         lock.acquire()
         yield i
         i += 1
         lock.release()


fgen = f()

def count3():
     for _ in range(3):
         print '---', fgen.next()
     time.sleep(10)

start_daemon(count3)
time.sleep(1.0)
print "+++", fgen.next()


-- 
--Bryan



More information about the Python-list mailing list