[英]Python: Is there a way I can run mainloop() in the background?
Is there a way I can run mainloop() in the background? 有没有办法可以在后台运行mainloop()? I don't want to add the complexity of threads.
我不想增加线程的复杂性。 Thanks in advance.
提前致谢。
否。GUI框架的主循环必须始终在前台运行。
No, you can't do this. 不,你不能这样做。 The message loop runs in its thread and processes your message queue.
消息循环在其线程中运行并处理您的消息队列。
There isn't really any official distinction between background and foreground threads. 后台线程和前台线程之间实际上并没有任何正式的区别。 The thread that processes your message is typically called the foreground but it's not really any different from any other thread.
处理您的消息的线程通常称为前台线程,但它与其他任何线程实际上并没有什么不同。 It's only convention that leads us to refer to threads foreground or background.
唯一的约定使我们引用线程的前台或后台。
Ultimately, that main thread with the message loop in has the message loop at the top of its call stack and that's just the way it has to be. 最终,带有消息循环的主线程在其调用堆栈的顶部具有消息循环,这就是它必须这样做的方式。
It is possible to start a long running task and get it to frequently process messages, but this way requires a keen understanding of re-entrancy issues and often leads to insanity. 可以启动一个长期运行的任务并使其频繁处理消息,但是这种方式需要对重新进入问题有敏锐的理解,并经常导致精神错乱。
If you want long running background tasks then you probably need threads. 如果要长时间运行后台任务,则可能需要线程。
What about keeping your message loop in the foreground, and your other processing in the background (say, with an after method)? 如何将消息循环保持在前台,而将其他处理保持在后台(例如,使用after方法)呢? Especially if you don't want to use threads.
特别是如果您不想使用线程。 However, I think threads or processes will probably work better in the long run.
但是,我认为线程或进程从长远来看可能会更好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.