协程,又称微线程,纤程。英文名Coroutine。
协程是啥协程是python个中另外一种实现多任务的方式,只不过比线程更小占用更小执行单元(理解为需要的资源)。 为啥说它是一个执行单元,因为它自带CPU上下文。这样只要在合适的时机, 我们可以把一个协程 切换到另一个协程。 只要这个过程中保存或恢复 CPU上下文那么程序还是可以运行的。
通俗的理解:在一个线程中的某个函数,可以在任何地方保存当前函数的一些临时变量等信息,然后切换到另外一个函数中执行,注意不是通过调用函数的方式做到的,并且切换的次数以及什么时候再切换到原来的函数都由开发者自己确定。
协程和线程差异在实现多任务时, 线程切换从系统层面远不止保存和恢复 CPU上下文这么简单。 操作系统为了程序运行的高效性每个线程都有自己缓存Cache等等数据,操作系统还会帮你做这些数据的恢复操作。 所以线程的切换非常耗性能。但是协程的切换只是单纯的操作CPU的上下文,所以一秒钟切换个上百万次系统都抗的住。
简单实现协程 import time def test1(): while True: print("--test1--") yield time.sleep(0.5) def test2(): while True: print("--test2--") yield time.sleep(0.5) if __name__ == "__main__": t1 = test1() t2 = test2() while True: next(t1) next(t2) greenlet 安装方式 sudo pip3 install greenlet import time from greenlet import greenlet def test1(): while True: print("--test1--") g2.switch() time.sleep(0.5) def test2(): while True: print("--test2--") g1.switch() time.sleep(0.5) g1 = greenlet(test1) g2 = greenlet(test2) g1.switch()运行效果:
--test1-- --test2-- --test1-- --test2-- --test1-- --test2-- --test1-- --test2-- --test1-- 。。。 geventgreenlet已经实现了协程,但是这个还的人工切换,是不是觉得太麻烦了,不要捉急,python还有一个比greenlet更强大的并且能够自动切换任务的模块gevent
其原理是当一个greenlet遇到IO(指的是input output 输入输出,比如网络、文件操作等)操作时,比如访问网络,就自动切换到其他的greenlet,等到IO操作完成,再在适当的时候切换回来继续执行。
由于IO操作非常耗时,经常使程序处于等待状态,有了gevent为我们自动切换协程,就保证总有greenlet在运行,而不是等待IO。
安装 pip3 install gevent 1. gevent的使用 import gevent def test(n): for i in range(n): print(gevent.getcurrent(), i) g1 = gevent.spawn(test, 5) g2 = gevent.spawn(test, 5) g3 = gevent.spawn(test, 5) g1.join() g2.join() g3.join() 2. gevent切换执行 import gevent def test(n): for i in range(n): print(gevent.getcurrent(), i) gevent.sleep(0.5) g1 = gevent.spawn(test, 5) g2 = gevent.spawn(test, 5) g3 = gevent.spawn(test, 5) g1.join() g2.join() g3.join()运行结果:
<Greenlet "Greenlet-0" at 0x7f04bfffb748: test(5)> 0 <Greenlet "Greenlet-1" at 0x7f04bfffb948: test(5)> 0 <Greenlet "Greenlet-2" at 0x7f04bfffba48: test(5)> 0 <Greenlet "Greenlet-0" at 0x7f04bfffb748: test(5)> 1 <Greenlet "Greenlet-1" at 0x7f04bfffb948: test(5)> 1 <Greenlet "Greenlet-2" at 0x7f04bfffba48: test(5)> 1 <Greenlet "Greenlet-0" at 0x7f04bfffb748: test(5)> 2 <Greenlet "Greenlet-1" at 0x7f04bfffb948: test(5)> 2 <Greenlet "Greenlet-2" at 0x7f04bfffba48: test(5)> 2 <Greenlet "Greenlet-0" at 0x7f04bfffb748: test(5)> 3 <Greenlet "Greenlet-1" at 0x7f04bfffb948: test(5)> 3 <Greenlet "Greenlet-2" at 0x7f04bfffba48: test(5)> 3 <Greenlet "Greenlet-0" at 0x7f04bfffb748: test(5)> 4 <Greenlet "Greenlet-1" at 0x7f04bfffb948: test(5)> 4 <Greenlet "Greenlet-2" at 0x7f04bfffba48: test(5)> 4 3. 给程序打补丁 import time import gevent def test(n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.5) g1 = gevent.spawn(test, 5) g2 = gevent.spawn(test, 5) g3 = gevent.spawn(test, 5) g1.join() g2.join() g3.join()运行结果:
<Greenlet "Greenlet-0" at 0x7f1a8dde6748: test(5)> 0 <Greenlet "Greenlet-0" at 0x7f1a8dde6748: test(5)> 1 <Greenlet "Greenlet-0" at 0x7f1a8dde6748: test(5)> 2 <Greenlet "Greenlet-0" at 0x7f1a8dde6748: test(5)> 3 <Greenlet "Greenlet-0" at 0x7f1a8dde6748: test(5)> 4 <Greenlet "Greenlet-1" at 0x7f1a8dde6948: test(5)> 0 <Greenlet "Greenlet-1" at 0x7f1a8dde6948: test(5)> 1 <Greenlet "Greenlet-1" at 0x7f1a8dde6948: test(5)> 2 <Greenlet "Greenlet-1" at 0x7f1a8dde6948: test(5)> 3 <Greenlet "Greenlet-1" at 0x7f1a8dde6948: test(5)> 4 <Greenlet "Greenlet-2" at 0x7f1a8dde6a48: test(5)> 0 <Greenlet "Greenlet-2" at 0x7f1a8dde6a48: test(5)> 1 <Greenlet "Greenlet-2" at 0x7f1a8dde6a48: test(5)> 2 <Greenlet "Greenlet-2" at 0x7f1a8dde6a48: test(5)> 3 <Greenlet "Greenlet-2" at 0x7f1a8dde6a48: test(5)> 4 import time import gevent from gevent import monkey monkey.patch_all() # 讲程序中用到的耗时的代码,换为gevent中实现的代码 def test(n): for i in range(n): print(gevent.getcurrent(), i) time.sleep(0.5) # g1 = gevent.spawn(test, 5) # g2 = gevent.spawn(test, 5) # g3 = gevent.spawn(test, 5) # g1.join() # g2.join() # g3.join() gevent.joinall([ gevent.spawn(test, 5), gevent.spawn(test, 5), gevent.spawn(test, 5) ])