常用的异步IO模块

asyncio

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持,更多信息

import asyncio
import requests


@asyncio.coroutine
def fetch_async(func, *args):
    loop = asyncio.get_event_loop()
    future = loop.run_in_executor(None, func, *args)
    response = yield from future
    print(response.url, response.content)


tasks = [
    fetch_async(requests.get, 'http://www.cnblogs.com/wupeiqi/'),
    fetch_async(requests.get, 'http://dig.chouti.com/pic/show?nid=4073644713430508&lid=10273091')
]

loop = asyncio.get_event_loop()
results = loop.run_until_complete(asyncio.gather(*tasks))
loop.close()

 gevent

Python通过yield提供了对协程的基本支持,但是不完全。而第三方的gevent为Python提供了比较完善的协程支持.更多信息

import gevent

import requests
from gevent import monkey

monkey.patch_all()


def fetch_async(method, url, req_kwargs):
    print(method, url, req_kwargs)
    response = requests.request(method=method, url=url, **req_kwargs)
    print(response.url, response.content)

# ##### 发送请求 #####
gevent.joinall([
    gevent.spawn(fetch_async, method='get', url='https://www.python.org/', req_kwargs={}),
    gevent.spawn(fetch_async, method='get', url='https://www.yahoo.com/', req_kwargs={}),
    gevent.spawn(fetch_async, method='get', url='https://github.com/', req_kwargs={}),
])

# ##### 发送请求(协程池控制最大协程数量) #####
# from gevent.pool import Pool
# pool = Pool(None)
# gevent.joinall([
#     pool.spawn(fetch_async, method='get', url='https://www.python.org/', req_kwargs={}),
#     pool.spawn(fetch_async, method='get', url='https://www.yahoo.com/', req_kwargs={}),
#     pool.spawn(fetch_async, method='get', url='https://www.github.com/', req_kwargs={}),
# ])

twisted

Twisted是基于事件驱动的网络引擎框架

from twisted.web.client import getPage, defer
from twisted.internet import reactor

def all_done(arg):
	reactor.stop()

def callback(contents):
	print(contents)


d_list = []
url_list = ['http://www.bing.com', 'http://www.baidu.com', ]
for url in url_list:
	d = getPage(bytes(url, encoding='utf8'))
	d.addCallback(callback)

	d_list.append(d)
# 用于检查是否页面已经全部下载完成,如果已下载完成那么,就停止循环。
dlist = defer.DeferredList(d_list)
dlist.addBoth(all_done) 

reactor.run()

  

posted @ 2018-05-10 20:19  瓜田月夜  阅读(120)  评论(0)    收藏  举报