This is because time.sleep()
isn't gevent-aware, so when join()
is called, the execution flow will be:
gevent.spawn(a)
— pushes a "spawn a" operation onto the event queuegevent.spawn(b)
— pushes a "spawn b" operation onto the event queue.join()
— causes the main thread to yield and next event in the event queue is executed (in this case,a
)a
executestime.sleep(4)
, blocking the entire process for 4 seconds (but threada
doesn't yield becausetime.sleep()
isn't gevent-aware)a
terminates and the next event in the event queue is executed (in this case,b
)b
executes and terminates, and the next event on the queue is executed (in this case, jumping back into the main thread, causing the.join()
to return)
Use either gevent.monkey or gevent.sleep()
to see this perform as you'd expect.