class OrderStreamer:
def __init__(á¬):
á¬.terminate_flag = False
# worker thread to receive data stream
á¬.worker_thread = threading.Thread(
target=á¬.worker_thread_func,
daemon=True
)
def start_streaming(á¬, from_scheduler = False):
á¬.worker_thread.start()
def terminate(á¬):
á¬.terminate_flag = True
def worker_thread_func(á¬):
asyncio.run(á¬.aio_func()) # blocks
async def aio_func(á¬):
async with \
aiohttp.ClientSession() as session, \
session.ws_connect(streams_url) as wsock, \
anyio.create_task_group() as tg:
async for msg in wsock:
print(msg.data)
if á¬.terminate_flag:
await wsock.close()
terminate_flag
永远不会离开。
我尝试创建对runloop和websocket的外部引用:
async with \
aiohttp.ClientSession() as session, \
session.ws_connect(streams_url) as wsock, \
anyio.create_task_group() as tg:
á¬.wsock = wsock
á¬.loop = asyncio.get_event_loop()
... 修改我的终止函数:
def terminate(á¬):
# á¬.loop.stop()
asyncio.set_event_loop(á¬.loop)
async def kill():
await á¬.wsock.close()
asyncio.run(kill())
... 但它不起作用。