为什么GUI在与异步IO循环对话时应使用call_soon_threadsafe()

2024-04-29 21:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

在几个答案中(请参见herehere),当在单独的线程中处理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循环的队列


Tags: thetoloopeventasynciohere队列it
1条回答
网友
1楼 · 发布于 2024-04-29 21:37:01

没有合适的队列类可供使用asyncio.Queue不安全,无法从事件循环外部与之交互,queue.Queue将阻止事件循环

如果仍然要使用队列,可以使用asyncio.run_coroutine_threadsafe调用asyncio.Queueput方法

相关问题 更多 >