协程

协程,又称微线程,纤程。英文名Coroutine。一句话说明什么是线程:协程是一种用户态的轻量级线程。实现单线程的并发。

协程拥有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈。因此:

协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),每次过程重入时,就相当于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。

 

协程的好处:

  • 无需线程上下文切换的开销
  • 无需原子操作锁定及同步的开销(协程本身就是单线程,单线程操作是原子的不需要加锁)
    •   "原子操作(atomic operation)是不需要synchronized",所谓原子操作是指不会被线程调度机制打断的操作;这种操作一旦开始,就一直运行到结束,中间不会有任何 context switch (切换到另一个线程)。原子操作可以是一个步骤,也可以是多个操作步骤,但是其顺序是不可以被打乱,或者切割掉只执行部分。视作整体是原子性的核心。
  • 方便切换控制流,简化编程模型
  • 高并发+高扩展性+低成本:一个CPU支持上万的协程都不是问题。所以很适合用于高并发处理。

缺点:

  • 无法利用多核资源:协程的本质是个单线程,它不能同时将 单个CPU 的多个核用上,协程需要和进程配合才能运行在多CPU上.当然我们日常所编写的绝大部分应用都没有这个必要,除非是cpu密集型应用。
  • 进行阻塞(Blocking)操作(如IO时)会阻塞掉整个程序

协程如何实现在单线程里面多并发的效果? 其核心就是遇到 IO操作就切换,那么怎么知道 IO 操作已完成,自动切换回去呢?回调

 

 

协程的实现方式:

yield实现协程

greenlet是一个用C实现的协程模块,相比与python自带的yield,它可以使你在任意函数之间随意切换,而不需把这个函数先声明为generator

Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程。 Greenlet全部运行在主程序操作系统进程的内部,但它们被协作式地调度

 

 

windows 下安装gevent:

 

https://pypi.python.org/pypi/greenlet/0.4.12 下载greenlet,gevent

pip3 install greenlet-0.4.12-cp35-cp35m-win_amd64.whl

 

 

gevent的使用:

join方法:等待协程执行完在退出,和多线程类似

from gevent import monkey
import time

monkey.patch_all()
import gevent
from  urllib.request import urlopen


def f(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))

start_time_1 = time.time()

i = 'http://www.cnblogs.com/alex3714/articles/5248247.html'
a= gevent.spawn(f,i)
a.join()    #等待gevent执行完毕,否则程序直接退出等不到结果。不加join() 程序直接退出和多线程类似


print("cost is %s..." % str(time.time() - start_time_1))

#执行结果:
GET: http://www.cnblogs.com/alex3714/articles/5248247.html
92210 bytes received from http://www.cnblogs.com/alex3714/articles/5248247.html.
cost is 9.593548774719238...

#不加join的结果:
cost is 0.003000020980834961...



#遇到IO阻塞时会自动切换任务

from
gevent import monkey import time monkey.patch_all() import gevent from urllib.request import urlopen def f(url): print('GET: %s' % url) resp = urlopen(url) data = resp.read() print('%d bytes received from %s.' % (len(data), url)) start_time = time.time() gevent.joinall([ #gevent.joinall 等待所有的协程都执行完毕后程序在退出或者接着往下走 gevent.spawn(f, 'http://www.cnblogs.com/alex3714/articles/5248247.html'), gevent.spawn(f, 'https://github.com/'), ]) print("cost is %s..." % str(time.time() - start_time))
#执行结果:
GET: http://www.cnblogs.com/alex3714/articles/5248247.html
GET: https://github.com/
51594 bytes received from https://github.com/.
92210 bytes received from http://www.cnblogs.com/alex3714/articles/5248247.html.
cost is 9.431539297103882...

不加join方法,如何让协程不退出?

from gevent import monkey
import time

monkey.patch_all()
import gevent
from  urllib.request import urlopen


def g(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))


i = 'http://www.cnblogs.com/alex3714/articles/5248247.html'

while True:
    time.sleep(10)
    a = gevent.spawn(g, i)
    print("ok?")

#执行结果:
#首先等待10s,直接跳过a,输出ok。再次等待10s的过程中得到a的结果。因为没有join,通过循环保证线程不退出从而得到协程的结果

ok?
GET: http://www.cnblogs.com/alex3714/articles/5248247.html
92210 bytes received from http://www.cnblogs.com/alex3714/articles/5248247.html.
ok?
GET: http://www.cnblogs.com/alex3714/articles/5248247.html
View Code

相关文章:

  • 2021-05-18
  • 2022-01-12
  • 2021-10-05
  • 2021-12-11
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-05-19
  • 2021-08-26
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案