जबकि threading.Condition
डबल चेकिंग सही ढंग से बंदर है, मैंने देखा कि एक बंदरगाह threading.Thread(…).start()
gevent.spawn(…)
से अलग व्यवहार करता है।`gevent.spawn` एक बंदरगाह` थ्रेडिंग से अलग क्यों है। थ्रेड() `?
पर विचार करें:
from gevent import monkey; monkey.patch_all()
from threading import Thread, Condition
import gevent
cv = Condition()
def wait_on_cv(x):
cv.acquire()
cv.wait()
print "Here:", x
cv.release()
# XXX: This code yields "This operation would block forever" when joining the first thread
threads = [ gevent.spawn(wait_on_cv, x) for x in range(10) ]
"""
# XXX: This code, which seems semantically similar, works correctly
threads = [ Thread(target=wait_on_cv, args=(x,)) for x in range(10) ]
for t in threads:
t.start()
"""
cv.acquire()
cv.notify_all()
print "Notified!"
cv.release()
for x, thread in enumerate(threads):
print "Joining", x
thread.join()
ध्यान दें, विशेष रूप से, XXX
के साथ शुरू दो टिप्पणी नहीं।
Notified! Joining 0 Traceback (most recent call last): File "foo.py", line 30, in thread.join() File "…/gevent/greenlet.py", line 291, in join result = self.parent.switch() File "…/gevent/hub.py", line 381, in switch return greenlet.switch(self) gevent.hub.LoopExit: This operation would block forever
हालांकि, Thread(…).start()
(दूसरे खंड), सब कुछ उम्मीद के रूप में काम करता है:
पहली पंक्ति (gevent.spawn
के साथ) का उपयोग कर, पहले thread.join()
एक अपवाद को जन्म देती है।
यह क्यों होगा? gevent.spawn()
और Thread(…).start()
के बीच क्या अंतर है?