[Python-bugs-list] [ python-Bugs-452973 ] Tcl event loop callback woes

noreply@sourceforge.net noreply@sourceforge.net
Sun, 24 Feb 2002 09:26:37 -0800


Bugs item #452973, was opened at 2001-08-19 09:34
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=452973&group_id=5470

Category: Tkinter
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Dan Egnor (egnor)
Assigned to: Nobody/Anonymous (nobody)
Summary: Tcl event loop callback woes

Initial Comment:
I have C code which handles I/O and needs an event
dispatcher.  It is running in the context of a Python
GUI application, and uses the Tcl event loop, using
Tcl_CreateFileHandler and friends.  The C code gets
callbacks from the Tcl event loop fine, but when it
attempts to call into the Python application from one
of these callbacks, things go wrong.

- Tkinter has released the GIL and hidden the current
thread state.  I can work around this by re-acquiring
the GIL from the callback and creating a new thread state.

- When the callback is invoked, Tkinter's tcl_lock is
held.  If the Python code invoked from the callback
ultimately calls some other Tkinter function, the
tcl_lock is still held, and deadlock results.  The only
way to work around this is to use a single-threaded
Python build.

- If the Python code returns an error, there's no way
to stop the event loop to report the error up. 
Tkinter's error-reporting mechanisms are inaccessible.

In general, Tkinter has a lot of infrastructure for
managing callbacks from the Tcl event loop.  If a third
party C library wants to use the same event loop, that
infrastructure is unavailable, and it is very difficult
to work with Python.

Unfortunately, short of using threads (which have their
own problems), there's no other alternative for an
external C library to do I/O without blocking the GUI.
 I've seen several problem reports from people trying
to do exactly this, though they almost never figure out
all of what's going on, and nobody else ever has any
good advice to offer.

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-02-24 09:26

Message:
Logged In: YES 
user_id=21627

It seems that all you need to have is access to the Tcl
lock. Would that solve your problem?

As for reporting the error up: This is certainly possible.
Just implement a _report_exception method on your widget;
define it as

    def _report_exception(self):
        raise


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-08-20 11:32

Message:
Logged In: YES 
user_id=6380

Unassigning -- /F is a black hole. :-(

----------------------------------------------------------------------

Comment By: Barry Warsaw (bwarsaw)
Date: 2001-08-20 10:02

Message:
Logged In: YES 
user_id=12800

For lack of a volunteer or better victim... er, assigning to /F

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=452973&group_id=5470