【发布时间】:2021-01-03 08:05:54
【问题描述】:
所以很抱歉,因为我看到这个问题问了很多,但是查看了所有问题似乎都没有解决我的问题。我的代码是这样的
TDSession = TDClient()
TDSession.grab_refresh_token()
q = queue.Queue(10)
asyncio.run(listener.startStreaming(TDSession, q))
while True:
message = q.get()
print('oh shoot!')
print(message)
orderEntry.placeOrder(TDSession=TDSession)
我试过asyncio.create_task(listener.startStreaming(TDSession,q)),问题是我得到了
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'startStreaming' was never awaited
这让我很困惑,因为这似乎在这里工作 Can an asyncio event loop run in the background without suspending the Python interpreter? 这是我想要做的。
listener.startStreaming 函数看起来像这样
async def startStreaming(TDSession, q):
streamingClient = TDSession.create_streaming_session()
streamingClient.account_activity()
await streamingClient.build_pipeline()
while True:
message = await streamingClient.start_pipeline()
message = parseMessage(message)
if message != None:
print('putting message into q')
print( dict(message) )
q.put(message)
有没有办法让我可以在后台运行监听器?
编辑:我也试过这个,但它只运行 consumer 函数,而不是同时运行两者
TDSession.grab_refresh_token()
q = queue.Queue(10)
loop = asyncio.get_event_loop()
loop.create_task(listener.startStreaming(TDSession, q))
loop.create_task(consumer(TDSession, q))
loop.run_forever()
【问题讨论】:
标签: python-3.x async-await python-asyncio