[Python-Dev] PEP needed? Introducing Tcl objects

Jeff Hobbs JeffH@ActiveState.com
Mon, 18 Feb 2002 16:25:57 -0800


> I hope that it's possible to do something better with Tcl/Tk 8.3 that
> doesn't require the sleep and maintains the existing _tkinter API /
> semantics.

I guess I would need to get a better understanding of why it was
designed with the sleep in the first place.  Martin mentioned
that it allows a thread switch to occur, but a shorter sleep
interval would have done the same.

Tk 8.1+ has been thread-safe, but only in 8.3 have people been
pushing it a little harder (most users of threads are Tcl-only).
However, there are the different models between Python and Tcl
threading, and perhaps that is a reason another method wasn't
attempted early.

Anyway, as to Martin's questions:

> In the light of this rationale, can you please explain what
> Tcl_AsyncMark is and how it would avoid the problem, or what effect
> calling Tcl_CreateEventSource would have, or how Tcl_ThreadQueueEvent
> would help?

Tcl_AsyncMark is what you would call if you left the while loop
looking more like:

[global or tls]
static stopRequested = 0;

[in func]
	while (!stopRequested && foundEvent) {
	    foundEvent = Tcl_DoOneEvent(TCL_ALL_EVENTS);
	}

And whenever a signal occurs, you would do:

ProcessSignal()
{
    stopRequested = 1;
    Tcl_AsyncMark(asyncHandler);
}

The asyncHandler then has it's own callback routine of your
choosing.  Now this might not be what you want, as this is more
the design for single-threaded systems that want an event loop.

There is also the Tcl_CreateEventSource route.  This allows you
to provide a proc that gets called in addition to the internal
Tcl one for processing events.  This is most often used when
tieing together event sources like Tk and Gtk, or Tk and MFC, ...

You may simply need to call Tcl_SetMaxBlockTime.  This will
prevent Tcl from indefinitely blocking when no events are
received.  This may be the simplest solution to create the same
effect as the Sleep, but without any other negative effects.

Most all of these are described in fairly good detail here:
	http://www.tcl.tk/man/tcl8.3/TclLib/Notifier.htm

Jeff