python异步之asyncio

本文介绍了Python的异步库asyncio及其使用,展示了如何通过async/await语法编写并发代码,包括Hello World示例和网络请求。此外,还讲解了aiohttp库的安装及创建HTTP服务器的基本操作,帮助读者理解Python异步编程在实际应用中的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.简介

asyncio 是用来编写 并发 代码的库,使用 async/await 语法。

asyncio 被用作多个提供高性能 Python 异步框架的基础,包括网络和网站服务,数据库连接库,分布式任务队列等等。

asyncio 往往是构建 IO 密集型和高层级 结构化 网络代码的最佳选择。

二.asyncio

1.用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()

2.用asyncio的异步网络连接来获取百度、搜狗和360的网站首页:

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.0\r\nHost: %s\r\n\r\n' % host
    writer.write(header.encode('utf-8'))
    yield from writer.drain()
    while True:
        line = yield from 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()

loop = asyncio.get_event_loop()
tasks = [wget(host) for host in ['www.baidu.com.cn', 'www.sougou.com', 'www.360.com']]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()

async/await

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

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

1.把@asyncio.coroutine替换为async;
2. 把yield from替换为await。

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

aiohttp

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

1.安装

pip install aiohttp

2.然后编写一个HTTP服务器,分别处理URL

import asyncio

from aiohttp import web

async def index(request):
    await asyncio.sleep(0.5)
    return web.Response(body=b'<h1>Index</h1>')

async def hello(request):
    await asyncio.sleep(0.5)
    text = '<h1>hello, %s!</h1>' % 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()
### Python `asyncio` 异步编程教程 #### 三、定义协程函数 在Python中,可以通过`async def`语句来定义一个协程函数。当调用这样的函数时,它并不会立即执行而是返回一个可以被等待完成的协程对象。 ```python async def my_coroutine(): await asyncio.sleep(1) print("Coroutine executed") ``` 此段代码展示了如何创建简单的协程函数[^4]。 #### 四、使用事件循环运行单个协程 为了启动并运行协程直到其结束,在最基础的情况下可以直接利用`asyncio.run()`方法传递要执行的任务给默认事件循环处理: ```python import asyncio async def say_after(delay, what): await asyncio.sleep(delay) print(what) async def main(): print(f"started at {time.strftime('%X')}") await say_after(1, 'hello') await say_after(2, 'world') print(f"finished at {time.strftime('%X')}") # This line runs the event loop and waits until completion of all coroutines. asyncio.run(main()) ``` 这段程序会依次打印时间戳以及'hello'和'world'[^1]。 #### 五、并发执行多个协程 如果希望同时发起几个独立的操作而不需要等到前一个完成后才开始下一个,则应该考虑采用更高级的方式如通过`gather()`收集所有待办事项作为一组任务一起提交给事件循环去调度它们之间的执行顺序: ```python import asyncio async def factorial(name, number): f = 1 for i in range(2, number + 1): print(f"Task {name}: Compute factorial({i})...") await asyncio.sleep(1) f *= i print(f"Task {name}: factorial({number}) = {f}") async def main(): # Schedule three calls *concurrently*: await asyncio.gather( factorial("A", 2), factorial("B", 3), factorial("C", 4), ) await main() ``` 上述例子说明了怎样让不同名称的任务并行计算阶乘值[^3]。 #### 六、设置超时期限 有时候可能需要限制某个操作的最大允许持续时间;这时就可以借助于`timeout()`辅助工具来做这件事儿——一旦超过指定的时间就会抛出异常终止该过程,并且还可以尝试调整已设定好的时限长度而不必重建整个环境配置. ```python import asyncio async def coroutine1(): await asyncio.sleep(5) print('coroutine1 finished') async def main(): try: async with asyncio.timeout(1): await coroutine1() except TimeoutError as e: print(e) asyncio.run(main()) ``` 这里展示了一个带有超时机制的例子,其中任何未能及时完成的工作都会被捕获为特定类型的错误实例[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kuokay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值