潜水艇官方网站代理,深圳建设工程交易网官网,无锡网站科技公司,网站建设需求多少钱大概aiohttpasyncio可以实现单线程并发IO操作。如果仅用在客户端#xff0c;发挥的威力不大。如果把asyncio用在服务器端#xff0c;例如Web服务器#xff0c;由于HTTP连接就是IO操作#xff0c;因此可以用单线程coroutine实现多用户的高并发支持。asyncio实现了TCP、UDP、SSL等… aiohttpasyncio可以实现单线程并发IO操作。如果仅用在客户端发挥的威力不大。如果把asyncio用在服务器端例如Web服务器由于HTTP连接就是IO操作因此可以用单线程coroutine实现多用户的高并发支持。asyncio实现了TCP、UDP、SSL等协议aiohttp则是基于asyncio实现的HTTP框架。我们先安装aiohttppip install aiohttp然后编写一个HTTP服务器分别处理以下URL/ - 首页返回bh1Index/h1/hello/{name} - 根据URL参数返回文本hello, %s!。代码如下import asyncio
from aiohttp import webasync def index(request):await asyncio.sleep(0.5) return web.Response(bodybh1Index/h1)async def hello(request):await asyncio.sleep(0.5)text h1hello, %s!/h1 % request.match_info[name] return web.Response(bodytext.encode(utf-8))async def init(loop):app web.Application(looploop)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 srvloop asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()注意aiohttp的初始化函数init()也是一个coroutineloop.create_server()则利用asyncio创建TCP服务。 转载于:https://blog.51cto.com/9130745/1743008