Python爬虫技术–基础篇–异步IO(下)-python 异步io

294次阅读
没有评论
Python爬虫技术--基础篇--异步IO(下)-python

1.asyncio

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

​​asyncio​​​的编程模型就是一个消息循环。我们从​​asyncio​​​模块中直接获取一个​​EventLoop​​​的引用,然后把需要执行的协程扔到​​EventLoop​​中执行,就实现了异步IO。

用​​asyncio​​​实现​​Hello world​​代码如下:

import asyncio

@asyncio.coroutine
def hello():
print(“Hello world!”)
# 异步调用asyncio.sleep(1):
r = yield from asyncio.sleep(1)
print(“Hello again!”)

# 获取EventLoop:
loop = asyncio.get_event_loop()
# 执行coroutine
loop.run_until_complete(hello())
loop.close()

​​@asyncio.coroutine​​​把一个generator标记为coroutine类型,然后,我们就把这个​​coroutine​​​扔到​​EventLoop​​中执行。

​​hello()​​​会首先打印出​​Hello world!​​​,然后,​​yield from​​​语法可以让我们方便地调用另一个​​generator​​​。由于​​asyncio.sleep()​​​也是一个​​coroutine​​​,所以线程不会等待​​asyncio.sleep()​​​,而是直接中断并执行下一个消息循环。当​​asyncio.sleep()​​​返回时,线程就可以从​​yield from​​​拿到返回值(此处是​​None​​),然后接着执行下一行语句。

把​​asyncio.sleep(1)​​​看成是一个耗时1秒的IO操作,在此期间,主线程并未等待,而是去执行​​EventLoop​​​中其他可以执行的​​coroutine​​了,因此可以实现并发执行。

我们用Task封装两个​​coroutine​​试试:

import threading
import asyncio

@asyncio.coroutine
def hello():
print(‘Hello world! (%s)’ % threading.currentThread())
yield from asyncio.sleep(1)
print(‘Hello again! (%s)’ % threading.currentThread())

loop = asyncio.get_event_loop()
tasks = [hello(), hello()]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

观察执行过程:

Hello world! (<_MainThread(MainThread, started 140735195337472)>)
Hello world! (<_MainThread(MainThread, started 140735195337472)>)
(暂停约1秒)
Hello again! (<_MainThread(MainThread, started 140735195337472)>)
Hello again! (<_MainThread(MainThread, started 140735195337472)>)

由打印的当前线程名称可以看出,两个​​coroutine​​是由同一个线程并发执行的。

如果把​​asyncio.sleep()​​​换成真正的IO操作,则多个​​coroutine​​就可以由一个线程并发执行。

我们用​​asyncio​​的异步网络连接来获取sina、sohu和163的网站首页:

import asyncio

@asyncio.coroutine
def wget(host):
print(‘wget %s…’ % host)
connect = asyncio.open_connection(host, 80)
reader, writer = yield from connect
header = ‘GET / HTTP/1.0rnHost: %srnrn’ % host
writer.write(header.encode(‘utf-8’))
yield from writer.drain()
while True:
line = yield from reader.readline()
if line == b’rn’:
break
print(‘%s header > %s’ % (host, line.decode(‘utf-8’).rstrip()))
# Ignore the body, close the socket
writer.close()

loop = asyncio.get_event_loop()
tasks = [wget(host) for host in [‘www.sina.com.cn’, ‘www.sohu.com’, ‘www.163.com’]]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

执行结果如下:

wget www.sohu.com…
wget www.sina.com.cn…
wget www.163.com…
(等待一段时间)
(打印出sohu的header)
www.sohu.com header > HTTP/1.1 200 OK
www.sohu.com header > Content-Type: text/html

(打印出sina的header)
www.sina.com.cn header > HTTP/1.1 200 OK
www.sina.com.cn header > Date: Wed, 20 May 2015 04:56:33 GMT

(打印出163的header)
www.163.com header > HTTP/1.0 302 Moved Temporarily
www.163.com header > Server: Cdn Cache Server V2.0

可见3个连接由一个线程通过​​coroutine​​并发完成。

小结

​​asyncio​​提供了完善的异步IO支持;

异步操作需要在​​coroutine​​​中通过​​yield from​​完成;

多个​​coroutine​​可以封装成一组Task然后并发执行。

 

 

2.async/await

用​​asyncio​​​提供的​​@asyncio.coroutine​​​可以把一个generator标记为coroutine类型,然后在coroutine内部用​​yield from​​调用另一个coroutine实现异步操作。

为了简化并更好地标识异步IO,从Python 3.5开始引入了新的语法​​async​​​和​​await​​,可以让coroutine的代码更简洁易读。

请注意,​​async​​​和​​await​​是针对coroutine的新语法,要使用新的语法,只需要做两步简单的替换:

  • 把​​@asyncio.coroutine​​​替换为​​async​​;
  • 把​​yield from​​​替换为​​await​​。
  • 让我们对比一下上一节的代码:

    @asyncio.coroutine
    def hello():
    print(“Hello world!”)
    r = yield from asyncio.sleep(1)
    print(“Hello again!”)

    用新语法重新编写如下:

    async def hello():
    print(“Hello world!”)
    r = await asyncio.sleep(1)
    print(“Hello again!”)

    剩下的代码保持不变。

    小结

    Python从3.5版本开始为​​asyncio​​​提供了​​async​​​和​​await​​的新语法;

    注意新语法只能用在Python 3.5以及后续版本,如果使用3.4版本,则仍需使用上一节的方案。

     

     

    3.aiohttp

    ​​asyncio​​​可以实现单线程并发IO操作。如果仅用在客户端,发挥的威力不大。如果把​​asyncio​​​用在服务器端,例如Web服务器,由于HTTP连接就是IO操作,因此可以用单线程+​​coroutine​​实现多用户的高并发支持。

    ​​asyncio​​​实现了TCP、UDP、SSL等协议,​​aiohttp​​​则是基于​​asyncio​​实现的HTTP框架。

    我们先安装​​aiohttp​​:

    pip install aiohttp

    然后编写一个HTTP服务器,分别处理以下URL:

    • ​​/​​​ – 首页返回​​b'<h1>Index</h1>’​​;
    • ​​/hello/{name}​​​ – 根据URL参数返回文本​​hello, %s!​​。

    代码如下:

    import asyncio

    from aiohttp import web

    async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body=b’Index’)

    async def hello(request):
    await asyncio.sleep(0.5)
    text = ‘hello, %s!’ % request.match_info[‘name’]
    return web.Response(body=text.encode(‘utf-8’))

    async def init(loop):
    app = web.Application(loop=loop)
    app.router.add_route(‘GET’, ‘/’, index)
    app.router.add_route(‘GET’, ‘/hello/{name}’, hello)
    srv = await loop.create_server(app.make_handler(), ‘127.0.0.1’, 8000)
    print(‘Server started at http://127.0.0.1:8000…’)
    return srv

    loop = asyncio.get_event_loop()
    loop.run_until_complete(init(loop))
    loop.run_forever()

    注意​​aiohttp​​​的初始化函数​​init()​​​也是一个​​coroutine​​​,​​loop.create_server()​​​则利用​​asyncio​​创建TCP服务。

    神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

    相关文章:

    版权声明:Python教程2022-11-01发表,共计4304字。
    新手QQ群:570568346,欢迎进群讨论 Python51学习