Python 3.x aiohttp和asyncio如何以并发方式从http请求和websocket获取响应?
我尝试同时从两个端点接收数据。但如果websocket停止发送消息,我将不会从Python 3.x aiohttp和asyncio如何以并发方式从http请求和websocket获取响应?,python-3.x,websocket,python-asyncio,aiohttp,Python 3.x,Websocket,Python Asyncio,Aiohttp,我尝试同时从两个端点接收数据。但如果websocket停止发送消息,我将不会从的请求中接收数据。”https://www.blabla.com“。解决这个问题的最好办法是什么 import asyncio import aiohttp URL = 'wss://www.some_web_socket.io' async def get_some_data(session): url = "https://www.blabla.com" async with session.
的请求中接收数据。”https://www.blabla.com“
。解决这个问题的最好办法是什么
import asyncio
import aiohttp
URL = 'wss://www.some_web_socket.io'
async def get_some_data(session):
url = "https://www.blabla.com"
async with session.get(url) as response:
data = await response.text()
return data
async def ws_handler(url):
async with aiohttp.ClientSession() as session:
async with session.ws_connect(url) as ws:
msg = await ws.receive()
while True:
some_data_from_get_request = await get_some_data(session)
msg_from_websocket = await ws.receive()
if msg.type == aiohttp.WSMsgType.TEXT:
print(stream_data)
print(some_data_from_get_request)
def _main():
asyncio.run(ws_handler(URL))
if __name__ == "__main__":
_main()
此代码序列化HTTP和websocket通信的返回值:
while True:
some_data_from_get_request = await get_some_data(session)
msg_from_websocket = await ws.receive()
为了能够检测两个协同程序中的任何一个返回,您可以使用asyncio.wait(…,return\u when=asyncio.FIRST\u COMPLETED)
:
http_fut = asyncio.ensure_future(get_some_data(session))
ws_fut = asyncio.ensure_future(ws.receive())
pending = {http_fut, ws_fut}
while pending:
_done, pending = await asyncio.wait(pending, return_when=asyncio.FIRST_COMPLETED)
if http_fut.done():
some_data_from_get_request = http_fut.result()
...
if ws_fut.done():
msg_from_websocket = ws_fut.result()
...