TK + cmd.cmdloop() in threads (was Re: howdoI: Exception in thread - terminating the program?

Ville Vainio vvainio at karhu.tp.spt.fi
Wed Jul 18 02:02:09 EDT 2001


David Bolen <db3l at fitlinxx.com> writes:

> If your main thread isn't going to be doing anything but spinning up
> the other threads, then having it wait on such an event is fine -
> although you may want to consider having it read from a queue, and
> thus be able to not only receive information from failing threads (the
> failing thread could even send over the traceback object through the
> queue so the main thread could handle printing/logging duties for all
> operational threads), but also completion or other communication from
> properly working threads.

The architecture is indeed such that the main thread is just waiting
there. Having the main thread do the exception saving/printing sounds
like a good idea, I think I will do that in addition to the mentioned
event.

> If you're going to encapsulate this in a GUI at some point, then the
> threads can likely generate events to the main GUI thread which can
> also serve to communicate such information.

I haven't done any UI stuff (apart from cmd and toy GTK/TK examples)
in python yet, is it ok to have TK mainloop running in one thread and
still perform normal cmd.cmdloop() operation in another? I can't
quickly think of any reason why it wouldn't be, but I can't figure out
why it hasn't been explicitly suggested... Having a TK window doing
the data browsing/drawing while still having a straightforward,
versatile cmd.cmdloop() running on a console sounds like an extremely
potent combination. Anyone done it? Are there some subtle gotchas that
make it a bad idea?

> if you need to permit them to perform cleanup, you may actually want a
> path from the main thread to the worker threads (e.g., have them check
> a terminate flag or event periodically in their execution path) as well.

Having a terminate flag is problematic with some threads that are
doing something that blocks... if they are reading from a socket you
have to kill the socket under their feet to make them go over the read
instruction. They may also be blocking on a mutex or a semaphore,
which makes things harder.

-- 
Ville Vainio - http://www.tp.spt.fi/~vvainio - ICQ #115524762
 Wild geese have no intention to cast a reflection
 Water has no mind to assume their form



More information about the Python-list mailing list