在几个答案中(请参见here和here),当在单独的线程中处理GUI+asyncio时,建议在asyncio循环需要与GUI通信时使用队列。但是,当GUI想要与asyncio事件循环通信时,它应该使用call_soon_threadsafe()
例如,一个答复指出:
When the event loop needs to notify the GUI to refresh something, it can use a queue as shown here. On the other hand, if the GUI needs to tell the event loop to do something, it can call call_soon_threadsafe or run_coroutine_threadsafe.
我不明白的是,为什么GUI不能同时使用另一个队列而不是call_soon_threadsafe()
?i、 e.GUI不能将数据放在队列上,以便异步IO循环获取和处理吗?这仅仅是一个设计决策,还是有一些技术原因不使用从GUI到asyncio循环的队列
没有合适的队列类可供使用
asyncio.Queue
不安全,无法从事件循环外部与之交互,queue.Queue
将阻止事件循环如果仍然要使用队列,可以使用
asyncio.run_coroutine_threadsafe
调用asyncio.Queue
的put
方法相关问题 更多 >
编程相关推荐