54912

gevent 完成多任务_2

''' gevent来完成多任务,即协程 协程相对进程和线程来说,消耗的资源更加的小,任务切换比较快'''import geventfrom gevent import monkeyimport timemonkey.patch_all() # 处理延时的操作 否则出现time.sleep()的时候,gevent无法完成利用延时时间去执行别的任务class Gevent_test(object): def f1(self, n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.1) def f2(self, n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.1)if __name__ == '__main__': g = Gevent_test() g1 = gevent.spawn(g.f1, 5) g2 = gevent.spawn(g.f2, 5) gevent.joinall([ gevent.spawn(g.f1, 5), gevent.spawn(g.f2, 5) ])

来源:博客园

作者:Victor_JJ

链接:https://www.cnblogs.com/victorstudy/p/11425905.html

Recommend