python3.5增加了内置的async和wait关键字。让我们用几个例子来展示这两个关键词如何使用。
如果你不明白什么是“异步编程”,你可以简单的理解他为“在单个进程中同时处理多个任务的一种方法”。我们通常使用的是进程经常花费大量的实践的等待IO操作的完成。这种IO操作包括客户端请求网络、读取数据、查询数据库等等…同步程序一般是等待IO操作完成后再进行下一个任务,而异步程序则可以在IO操作期间去处理下一个任务。
下面看简单例子,这里我们用asyncio.sleep模拟耗时的IO操作,并用asyncio.wait让他们并行运行。在python3.5之前,代码大概是这样的:
import asyncio
@asyncio.coroutine
def show_operation(n):
yield from asyncio.sleep(1)
print("show operation {} complete".format(n))
@asyncio.coroutine
def main():
yield from asyncio.wait([
show_operation(1),
show_operation(2),
show_operation(3),
])
loop=asyncio.get_event_loop()
loop.run_until_complate(main())
这个例子使用了原有的python的语法:装饰器和yield关键词
如果使用python3.5的语法,代码大概是这样的:
import asyncio
async def show_operation(n):
yield from asyncio.sleep(1)
print("show operation {} complete".format(n))
async def main():
yield from asyncio.wait([
show_operation(1),
show_operation(2),
show_operation(3),
])
loop=asyncio.get_event_loop()
loop.run_until_complate(main())
新的async 的await关键字清楚的表明我们在写的是一个异步函数,而不是普通的生成器。
再来看一个更加复杂的例子,我们实现一个web服务器,这个web服务器本身还需要向其他服务发起请求。这种情况在监控网站经常发生。我们使用aiohttp,一个使用asyncio构建的服务器客户端套件。
import asyncio
import aiohttp
from aiohttp import web
import json
WEBSITES=['http://example.com/','http://dummy-a98x3.com/','http://example.net/']
async def handle(request):
coroutines=[aiohttp.request('get',website) for website in WEBSITES]
results=await asyncio.gather(*coroutines,return_exceptions=True)
response_data={
website:not isinstance(result,Exception) and result.status==200
for website,result in zip(WEBSITE,results)
}
body=json.dumps(response_data).encode('utf-8')
return web.Response(body=body,content_type="application/json")
loop=asyncio.get_event_loop()
app=web.Application(loop=loop)
app.router.add_route("GET",'/',handle)
server=loop.create_server(app.make_hanle(),'127.0.0.1',8000)
loop.run_until_complete(server)
try:
loop.run_forever()
exception KeyboardInterrupt:
pass