Medusa
Mike Clarkson
support@i...
Sat, 09 Dec 2000 18:05:05 -0500
I've been playing with Medusa and I wanted to see if I could
make a GUI for it. I looked at the tkinter.txt notes, but that
won't work with Python 2.0, and it's really groddy way of doing it.
(at least in old versions of Tk after would crash after a while).
In Python 2.0 the main event loop of Tkinter is in C, with threads
meshed into the loop, as Tk is in it's own thread with its own special lock.
If you compile with threads, then the code is (_tkinter.c Tkapp_MainLoop)
quitMainLoop = 0;
while (Tk_GetNumMainWindows() > threshold &&
!quitMainLoop &&
!errorInCmd)
{
int result;
#ifdef WITH_THREAD
Py_BEGIN_ALLOW_THREADS
PyThread_acquire_lock(tcl_lock, 1);
tcl_tstate = tstate;
result = Tcl_DoOneEvent(TCL_DONT_WAIT);
tcl_tstate = NULL;
PyThread_release_lock(tcl_lock);
if (result == 0)
Sleep(20);
Py_END_ALLOW_THREADS
#else
result = Tcl_DoOneEvent(0);
#endif
Tcl_DoOneEvent(TCL_DONT_WAIT) handles any existing events and returns,
nonzero if it did anything, whereas Tcl_DoOneEvent(0) does select
waiting for events.
The loop uses a 20 msec wait using Sleep, but on most systems, Sleep is
just a call to a null select, so you could mesh the asynccore with Tkinter
using your poll as a replacement for Sleep.
The only detail is how to pass the array of fd's for poll in. Presumably
you could check for the existence of a global variable, which is groddy,
or you could extend the arguments to the Python mainloop function
(it already takes one optional argument), to take a callable object.
by default this callable object would be time.sleep(20).
If you were using asynccore you make the callable object a call to poll
with it's fd's (or better still a continuation?). This would have the added
benefit of giving the user control over the loop granularity so that the
change might even be adopted into the Tkinter core.
Does this seem like a good way to go? I'm new to Python so I don't know how
to pass a callable function as an argument, but it shouldn't be more than a
few lines of C code.
Many thanks for medusa.
Mike.