【发布时间】:2021-09-19 05:05:26
【问题描述】:
我是异步函数和线程的新手,我正在尝试返回从 Web 套接字获取的一系列值,以传递给正在执行同步代码的另一个线程。在代码中,我还使用了多 Web 套接字方法。下面我给你看代码:
"""
This code is designed to run an asynchronous loop
with asyncio in a separate thread. This allows mixing
a synchronous code with an asynchronous one.
"""
import asyncio
from datetime import datetime
from threading import Thread
import websockets
from typing import Tuple, List, Iterable
import json
import time
URLS = [
"wss://stream.binance.com:9443/ws/xrpusdt@kline_1m",
"wss://stream.binance.com:9443/ws/btcusdt@kline_1m",
]
def start_background_loop(loop: asyncio.AbstractEventLoop):
asyncio.set_event_loop(loop)
loop.run_forever()
async def IndividualSubscription(url: str):
"""An individual subscription to each WebSocket is created"""
async with websockets.connect(url) as websocket:
data = await websocket.recv()
data = json.loads(data)
print('\n', data)
return data
async def Subscriptions(URLS: Iterable[str]):
"""All concurrent tickets are subscribed and all are combined
in a single coroutine."""
while True:
task = [asyncio.create_task(SuscripcionIndividual(url)) for url in URLS]
# All tasks are run in parallel
await asyncio.gather(*tareas)
#return tareas
def main():
loop = asyncio.new_event_loop()
t = Thread(target=start_background_loop, args=(loop,), daemon=True)
t.start()
task = asyncio.run_coroutine_threadsafe(Suscripciones(URLS), loop)
for i in task.result():
print(f"{i}")
#return tareas
def function():
for i in range(100):
print("This is out of asynchronous ", i)
time.sleep(1)
if __name__ == "__main__":
main()
T2 = Thread(target=function,)
T2.start()
我尝试将return 放入异步代码中,但通过这样做,异步循环只运行一次,而不是像我期望的那样连续运行。另外,我已经尝试过 .result() 而不是 .create_task() 的方法。是否可以从异步函数中返回值?
【问题讨论】:
-
你的 main 方法应该在调用
T2.start()之后等待线程完成使用T2.join()并从你的async函数使用r = await f()获取返回值 -
例如
result = await asyncio.gather(...)或return await asyncio.gather(...)
标签: python-3.x asynchronous websocket python-asyncio python-multithreading