Gtk idle_add not working?

I have a dual-threaded application: GUI and some background work. I am trying to send requests to the main thread to do GUI updates (move the progress bar), but it does not seem to work. I dropped it 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()

It should, I thought, print something standard error from the main / GUI stream, but nothing happens. And he also does not leave, therefore gtk.main_quithe is not called.

Also, adding more output to the stderracts weird. If I changed the stream 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 of output. It looks like some kind of race condition when the main stream is introduced gtk.main, but I don't know why it would be.

+3
3

:

gtk.gdk.threads_init() , - : gtk.main(). , gtk.main(), , .

(idle, ) gtk.gdk.threads_enter() GTK ( ) gtk.gdk.threads_leave(), .

gtk.gdk.threads_init(), , , PyGTK GIL, - , - aux. , ( gtk.main()) GIL. gtk.gdk.threads_init(), , mojo PyGTK GTK+. - gtk.gdk.threads_init(), , GTK +, glib, gobject .., .

0

glib glib. :

glib.threads_init()

glib.

+5

glib.timeout_add_seconds(1, idle) False idle() , 1 ? , .

EDIT:

" ", , , . , .

, GTK :

  • glib.threads_init() gobject.threads_init(), , , vanza.
  • gtk.gdk.threads_init(). , , gtk.main(). C gtk_init(), PyGTK, .
  • gtk.main() gtk.gdk.threads_enter() gtk.gdk.threads_leave().
  • GTK :

    • ,
    • ,

    gtk.gdk.threads_enter() gtk.gdk.threads_leave().

, , enter/leave, with gtk.gdk.lock: with -block.

, :

+3

Source: https://habr.com/ru/post/1761803/


All Articles