python关键字

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值