day10-3-协程之爬虫,socket

手动切换协程greenlet

from greenlet import greenlet
def test1():
    print(12)
    gr2.switch()#切换到test2
    print(34)
    gr2.switch()
def test2():
    print(56)
    gr1.switch()#切回test1上次位置
    print(78)

gr1 = greenlet(test1) #启动一个携程
gr2 = greenlet(test2)
gr1.switch()
# 12
# 56
# 34
# 78

自动io切换:

import gevent
def foo():
    print('Running in foo')
    gevent.sleep(2)
    print('Explicit context switch to foo again')
def bar():
    print('Explicit精确的 context内容 to bar')
    gevent.sleep(1)
    print('Implicit context switch back to bar')
def func3():
    print("running func3 ")
    gevent.sleep(0)
    print("running func3  again ")
gevent.joinall([
    gevent.spawn(foo), #生成,
    gevent.spawn(bar),
    gevent.spawn(func3),
])
# Running in foo
# Explicit精确的 context内容 to bar
# running func3 
# running func3  again 0
# Implicit context switch back to bars 1s
# Explicit context switch to foo again  2s

协程之gevent简单爬虫:

from urllib import request
import gevent,time
from gevent import monkey
monkey.patch_all() #把当前程序的所有的io操作给我单独的做上标记

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

urls = ['https://www.python.org/',
        'https://www.yahoo.com/',
        'https://github.com/' ]
time_start = time.time()
for url in urls:
    f(url)
print("同步cost",time.time() - time_start)

async_time_start = time.time()
gevent.joinall([
    gevent.spawn(f, 'https://www.python.org/'),
    gevent.spawn(f, 'https://www.yahoo.com/'),
    gevent.spawn(f, 'https://github.com/'),
])
print("异步cost",time.time() - async_time_start)

# on.org/
# 48948 bytes received from https://www.python.org/.
# GET: https://www.yahoo.com/
# 477521 bytes received from https://www.yahoo.com/.
# GET: https://github.com/
# 51375 bytes received from https://github.com/.
# 同步cost 6.178353548049927
# GET: https://www.python.org/
# GET: https://www.yahoo.com/
# GET: https://github.com/
# 48948 bytes received from https://www.python.org/.
# 51375 bytes received from https://github.com/.
# 477037 bytes received from https://www.yahoo.com/.
# 异步cost 5.017286777496338

 

posted @ 2017-11-07 07:44  雷大侠!  阅读(115)  评论(0编辑  收藏  举报