Threading question
Peter Hansen
peter at engcorp.com
Thu Apr 15 07:58:42 EDT 2004
Torsten Marek wrote:
> Peter Hansen schrieb:
>> The other threads are running in, well, other threads, so you
>> won't see any effect on the above thread when they end. Therefore
>> you can't get much mileage out of a finally clause. A .join()
>> is definitely the way to go here.
> I found another way to go for now, which works fine for me.
> Since the threading module sets sys.exitfunc, I just do:
>
> def my_exitfunc():
> global thread_wait
> thread_wait()
> # own stuff follows here...
>
> thread_wait = sys.exitfunc
> sys.exitfunc = my_exitfunc
>
> That's maybe not very clean, but it's just a minor script I wrote, so I
> don't want to waste to much time on it;-)
Good solution. You're actually taking advantage of the fact that
the main thread does a join() on all non-daemon threads automatically
as it exits. The source in threading.py for the "main thread"
can make illuminating reading...
-Peter
More information about the Python-list
mailing list