阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

使用asyncio

25次阅读
没有评论

共计 2737 个字符,预计需要花费 7 分钟才能阅读完成。

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

asyncio的编程模型就是一个消息循环。asyncio模块内部实现了 EventLoop,把需要执行的协程扔到EventLoop 中执行,就实现了异步 IO。

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

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

asyncio 实现 Hello world 代码如下:

import asyncio

async def hello():
    print("Hello world!")
    # 异步调用 asyncio.sleep(1):
    await asyncio.sleep(1)
    print("Hello again!")

asyncio.run(hello())

async把一个函数变成 coroutine 类型,然后,我们就把这个 async 函数扔到 asyncio.run() 中执行。执行结果如下:

Hello!
(等待约 1 秒)
Hello again!

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

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

上述 hello() 还没有看出并发执行的特点,我们改写一下,让两个 hello() 同时并发执行:

# 传入 name 参数:
async def hello(name):
    # 打印 name 和当前线程:
    print("Hello %s! (%s)" % (name, threading.current_thread))
    # 异步调用 asyncio.sleep(1):
    await asyncio.sleep(1)
    print("Hello %s again! (%s)" % (name, threading.current_thread))
    return name

asyncio.gather() 同时调度多个 async 函数:

async def main():
    L = await asyncio.gather(hello("Bob"), hello("Alice"))
    print(L)

asyncio.run(main())

执行结果如下:

Hello Bob! (<function current_thread at 0x10387d260>)
Hello Alice! (<function current_thread at 0x10387d260>)
(等待约 1 秒)
Hello Bob again! (<function current_thread at 0x10387d260>)
Hello Alice again! (<function current_thread at 0x10387d260>)
['Bob', 'Alice']

从结果可知,用 asyncio.run() 执行 async 函数,所有函数均由同一个线程执行。两个 hello() 是并发执行的,并且可以拿到 async 函数执行的结果(即 return 的返回值)。

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

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

import asyncio

async def wget(host):
    print(f"wget {host}...")
    # 连接 80 端口:
    reader, writer = await asyncio.open_connection(host, 80)
    # 发送 HTTP 请求:
    header = f"GET / HTTP/1.0\r\nHost: {host}\r\n\r\n"
    writer.write(header.encode("utf-8"))
    await writer.drain()

    # 读取 HTTP 响应:
    while True:
        line = await reader.readline()
        if line == b"\r\n":
            break
        print("%s header > %s" % (host, line.decode("utf-8").rstrip()))
    # Ignore the body, close the socket
    writer.close()
    await writer.wait_closed()
    print(f"Done {host}.")

async def main():
    await asyncio.gather(wget("www.sina.com.cn"), wget("www.sohu.com"), wget("www.163.com"))

asyncio.run(main())

执行结果如下:

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 个连接由一个线程并发执行 3 个 async 函数完成。

小结

asyncio提供了完善的异步 IO 支持,用 asyncio.run() 调度一个coroutine

在一个 async 函数内部,通过 await 可以调用另一个 async 函数,这个调用看起来是串行执行的,但实际上是由 asyncio 内部的消息循环控制;

在一个 async 函数内部,通过 await asyncio.gather() 可以并发执行若干个 async 函数。

参考源码

hello.py

gather.py

wget.py

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-08-07发表,共计2737字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中