threading problem

Brian Craft bcboy at thecraftstudio.com
Fri Feb 8 16:11:57 EST 2002


Hi all --

I'm trying to use pygtk and orbit-python. They won't work together for reasons
I don't fully understand, but seem to have something to do with the python
threading model.

Both of these libraries work by providing a wrapper to invoke a glib main loop.
Since both orbit and gtk use the glib main loop, they can work together. The
main loop gets events (corba or gtk) and calls into the python interpreter to
handle them.

Both of them attempt to handle threading by using PyEval_SaveThread(), etc.,
like so:

  o The script invokes orb.run(), or gtk.mainloop()
  o   PyEval_SaveThread() is called.
  o   g_main_run() is invoked via the corresponding library.
  o     An event occurs, and a callback is made into pygtk or orbit-python.
  o       PyEval_RestoreThread() is called.
  o       A python callback for the event is invoked.
  o       PyEval_SaveThread() is called.
  o     The handler returns control to glib main.
  o   An event may exit the glib main loop (if user invokes File->quit)
  o   PyEval_RestoreThread is called.
  o Control returns to the python script.

It appears to me that what's happening is PyEval_RestoreThread() is being
invoked with a bogus value. If you initialize both libraries and call
gtk.mainloop(), then pygtk stores the state info. Then when a corba event
occurs, orbit-python attempts to restore the state info (which is uninitialized
in that library) and barfs with a thread error on this code:

        dict = PyObject_GetAttrString(servant->impl,"__dict__");

I don't understand these calls at all, so I'm not sure how this should be
fixed. In addition, pygtk uses these sick and wrong constructs, like:

         if (counter == -INITIAL_LOCK_COUNT) {                               \
           PyThreadState *_save;                                             \
           Py_UNBLOCK_THREADS;                                               \
           g_static_private_set(&pythreadstate_key, _save, NULL);            \
         }                                                                   \
 
where a variable defined in a macro is being referenced, for extra clarity.

Anyone with a clue?

Btw, putting the main loop in python isn't an option, because orbit doesn't
support a polling mode.

b.c.




More information about the Python-list mailing list