gtk idle_add没有运行? [英] gtk idle_add not running?
问题描述
我有一个双线程应用程序:GUI和一些后台工作。我试图发送请求到主线程来做GUI更新(移动一个进度条),但它似乎不工作。我将它简化为一个非常简单的例子:
import pygtk
pygtk.require('2.0')
导入glib
导入gtk
导入线程
导入sys
导入时间
def idle():
)sys.stderr.write('Hello from another world.\\\
')
sys.stderr.flush()
gtk.main_quit()
$ b $ def another_thread() :
time.sleep(1)
glib.idle_add(空闲)
线程= threading.Thread(target = another_thread)
thread.start()
gtk.main()
我认为这应该从main / GUI线程,但没有任何反应。它也不会退出,所以 gtk.main_quit
没有被调用。
另外,更多的输出到 stderr
的行为很奇怪。如果我将线程的函数改为:
sys.stderr.write('---- \ n')
sys.stderr.write('---- \ n')
sys.stderr.flush()
sys.stderr.write('After.\\\
')
sys.stderr.flush()
我看到1,有时2行输出。它看起来像主线程进入 gtk.main
的某种竞争条件,但我不知道这是为什么。
调用 gtk .gdk.threads_init()
在执行任何其他操作之前:在启动线程之前,在输入 gtk.main()
之前。我认为在实际中,只需要在输入 gtk.main()
之前调用它,但很容易调用它,而所有内容都是单线程和简单的。 p>
在空闲回调( idle
,例如)中,调用 gtk.gdk .threads_enter()
在GTK的东西之前(很容易做到函数的顶部),并且调用 gtk.gdk.threads_leave()
,在函数结束之前。
gtk.gdk.threads_init()
似乎也告诉PyGTK不是在睡觉时握住GIL - 我想我错过了辅助输出。只是因为睡眠的主线程(睡在 gtk.main()
)仍然保持着GIL。 gtk.gdk.threads_init()
,据我所知,向PyGTK和GTK +灌输好的mojo。因此,即使启动一个不接触GTK +的线程,也不需要执行任何与glib,gobject等有关的操作,所以需要 gtk.gdk.threads_init()
。,只是做了基本的计算。
I have a two-thread application: GUI, and some background work. I'm trying to send requests to the main thread to do GUI updates (move a progress bar), but it doesn't seem to work. I've boiled it down to a really minimal example:
import pygtk
pygtk.require('2.0')
import glib
import gtk
import threading
import sys
import time
def idle():
sys.stderr.write('Hello from another world.\n')
sys.stderr.flush()
gtk.main_quit()
def another_thread():
time.sleep(1)
glib.idle_add(idle)
thread = threading.Thread(target=another_thread)
thread.start()
gtk.main()
This should, I thought, print something to standard error from the main/GUI thread, but nothing happens. And it doesn't quit, either, so gtk.main_quit
isn't being called.
Also, adding more output to stderr
acts weirdly. If I change the thread's function to:
sys.stderr.write('----\n')
sys.stderr.write('----\n')
sys.stderr.flush()
sys.stderr.write('After.\n')
sys.stderr.flush()
I see 1, sometimes 2 lines out output. It looks like some kind of race condition with the main thread entering gtk.main
, but I don't know why this would be.
The following makes the above work for me:
A call to gtk.gdk.threads_init()
before doing anything else: before starting threads, before entering gtk.main()
. I think in reality, this only needs to be called before entering gtk.main()
, but it is easy enough to call it while everything is single threaded and simple.
In the idle callback (idle
, in the example), a call to gtk.gdk.threads_enter()
before GTK stuff (easy to do just at the top of the function), and a call to gtk.gdk.threads_leave()
, before the end of the function.
gtk.gdk.threads_init()
seems to also tell PyGTK not hold the GIL when it goes to sleep - I think I was missing some output from the aux. thread in the example just because the sleeping main thread (sleeping in gtk.main()
) was still holding the GIL. gtk.gdk.threads_init()
, as far as I can tell, instills good mojo into PyGTK and GTK+. Because of this, gtk.gdk.threads_init()
is needed even if you launch a thread that doesn't touch GTK+, doesn't do anything with glib, gobject, etc., just does basic computation.
这篇关于gtk idle_add没有运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!