Tkinter:等待队列中的项目
我正在使用队列在后台线程和 Tk GUI 应用程序之间交换消息。目前,这是通过不时调用查询方法来完成的。
def read_queue(self):
try:
self.process(self.queue.get(False)) # non-blocking
except Queue.Empty:
pass
finally:
self.after(UPDATE_TIME, self.read_queue)
这种方法的问题是,如果 UPDATE_TIME
太大,应用程序处理新项目的速度将比可能的慢。如果它太小,Tk 会花费大部分时间检查队列,尽管它同时可以做其他事情。
有没有办法在新项目到达队列时自动触发 read_queue 方法? (当后台线程填充队列时,我当然可以调用 Tk 上的方法,但我担心这会给我带来一些并发问题 - 这就是我毕竟使用队列的原因。)
I’m using a queue to exchange messages between a background thread and a Tk GUI application. Currently, this is done by calling a query method every now and then.
def read_queue(self):
try:
self.process(self.queue.get(False)) # non-blocking
except Queue.Empty:
pass
finally:
self.after(UPDATE_TIME, self.read_queue)
The problem with this approach is that if UPDATE_TIME
is too large, the application will process new items slower than possible. If it is too small, Tk spends most of the time checking the queue although it could do other stuff in the meantime.
Is there a way to automatically trigger the read_queue
method whenever a new item arrives in the queue? (I could certainly call a method on Tk when the background thread fills the queue but I fear that this gives me some concurrency issues – that’s why I’m using queues after all.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种选择可能是 mtTkinter http://tkinter.unpythonic.net/wiki/mtTkinter
这是使用 event_generate 的另一个示例后台线程:
还提到以类似的方式使用 after_idle 。
即。 root.after_idle(时间更改)
One option might be mtTkinter http://tkinter.unpythonic.net/wiki/mtTkinter
Here is another example of using event_generate from a background thread:
There is also mention of using after_idle in a similar way.
ie. root.after_idle(timeChanged)
摘要:我不会使用“noob oddy's example code”——这是一种根本上有缺陷的方法。
我不是 python 专家,但“noob oddy”提供的示例代码(调用 root.event_generate(. ..)在后台线程内)似乎是一种“根本上有缺陷的方法”。即,互联网上有几篇文章指出“永远不要在‘GUI 线程’(通常是主线程)的上下文之外调用 Tkinter 函数/对象方法”。
他的示例“大部分时间”都有效,但如果增加事件生成率,则该示例的“崩溃率”将会增加——但是,具体行为取决于事件生成率和平台的性能特征。
例如,在 Python 2.7.3 中使用他的代码,如果将:更改
为:,
则脚本/应用程序通常会在“x”次迭代后崩溃。
经过大量搜索,如果您“必须使用 Tkinter”,那么从后台线程获取信息到 GUI 线程的最“防弹方法”似乎是使用 'after()' 小部件方法来轮询线程安全对象(例如“队列”)。例如,
SUMMARY: I wouldn't use "noob oddy's example code" -- is a fundamentally flawed approach.
I'm not a python guru, but the example code provided by "noob oddy" (which calls root.event_generate(...) within the background thread) appears to be a "fundamentally flawed approach". i.e., there are several articles on the internet which state "to never invoke Tkinter functions/object methods outside the context of the 'GUI thread'" (which is typically the main thread).
His example works "most of the time", but if you increase the event generation rate, then the example's "crash rate" will increase -- however, specific behavior is dependent on the event generation rate and the platform's performance characteristics.
For example, using his code with Python 2.7.3, if you change:
to:
then the script/app will typically crash after 'x' number of iterations.
After much searching, if you "must use Tkinter", then it appears the most "bullet proof method" of getting information from a background thread to the GUI thread is to use 'after()' widget method to poll a thread-safe object (such as 'Queue'). e.g.,
通过使用 os.pipe 在两个线程之间进行同步,可以从 Ken Mumme 解决方案中消除轮询。
tkinter 有一个 createFilehandler 方法,可用于将文件描述符添加到 tk 的选择循环中。然后,您可以通过向管道中写入一个字节来表示队列中的某些内容已准备就绪。
解决方案如下:
我不是Python专家;如果我搞砸了任何编码约定,我深表歉意。不过我对管道很在行:)
Polling can be eliminated from the Ken Mumme solution by using os.pipe to synchronise between the two threads.
tkinter has a createFilehandler method that can be used to add a file descriptor into tk's select loop. You can then signal that something is ready in the queue by writing a byte into the pipe.
The solution looks like this:
I'm not a python expert; apologies if I've messed up on any coding conventions. I'm great with pipes, though :)