python asyncio 服务器_python aiohttp服务器和带有asyncio的客户端

我正在运行一个aiohttp服务器 . 它一个接一个地要求,并且工作正常 .

from aiohttp import web

async def hello(request):

return web.Response(text='done')

app = web.Application()

app.add_routes([web.get('/', hello)])

web.run_app(app)

现在我需要逐个发出两个慢速传出请求 . 我想将aiohttp客户端代码放入hello函数中 . 因此,当服务器等待返回的出站请求时,它可以为下一个入站请求提供服务 .

async def hello(request):

async def fetch(session, url):

with async_timeout.timeout(10):

async with session.get(url) as response:

return await response.text()

async def main(loop):

async with aiohttp.ClientSession(loop=loop) as session:

html1 = await fetch(session, URL1)

html2 = await fetch(session, URL2)

loop = asyncio.get_event_loop()

loop.run_until_complete(main(loop))

return web.Response(text='done')

上面的html1和html2会同时出现 . 第一次提取完成后如何进行第二次提取?或者其他一些设计可以帮忙吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值