[pygtk] pygtk-1.2 and threading... Q&A

Andrew W. Schmeder andy@e-molecule.com
Thu, 3 Oct 2002 17:27:54 -0700 (PDT)


Hi.

I am writing a pygtk-1.2 app.  I have lots of interaction with GnomeCanvas and
lots of threading.  For example, it is common to have a thread running which
moves, creates or destroys a widget on the canvas.

In the process of doing this I encountered a number of non-deterministic,
intermitant, severe crashes related to threading problems.  Even after
extensive locking they would not go away.

1) While moving an object from a thread
GnomeUI-ERROR **: file gnome-canvas.c: line 3812
(gnome_canvas_request_redraw_uta): assertion failed: (canvas->redraw_area !=
NULL)
aborting...

2) While creating an object from a thread
Xlib: unexpected async reply (sequence 0x10b9)!

(plus various other sequence numbers)

The good news is that after some thought I came up with the following solution:
I replaced the standard gtk.mainloop() with:

while 1:
  App.gtk_lock.acquire()
  while gtk.events_pending() and not App.quit:
    gtk.mainiteration()
  App.gtk_lock.release()
  if App.quit:
    break
  time.sleep(0.01)

As long as all threaded code uses App.gtk_lock around any access to
pygtk/gnome structures, the threading errors are prevented.

I hope this may be useful to anyone else who is trying to wrangle with threads
and pygtk/pygnome.


  --andy

-----------------------
Andrew W. Schmeder
andy@e-molecule.com