from gevent import monkey;monkey.patch_all()import timeimport geventdef eat(): print("eating start") time.sleep(2) print("eating end")def play(): print("playing start") gevent.sleep(1) print("playing end")g1 = gevent.spawn(eat)g2 = gevent.spawn(play)g1.join()g2.join()输出结果:eating startplaying startplaying endeating end
协程:能够在一个线程中实现并发的效果,能够规避一些任务中的IO操作,在任务中的执行过程中,检测到IO就切换到其他任务