[Python-Dev] threading issue: preventing starting twice

Russell E. Owen rowen at cesmail.net
Wed Feb 25 14:29:40 EST 2004


In the threading module, one must of course only start a thread once. I 
have some questions about handling this gracefully.

First of all, the documentation only says it is an error to start a 
thread more than once. It does not say which exception is raised. On my 
system I find it is AssertionError. So...is that going to be true on all 
systems? Is the test based on "assert", in which case it will go away 
for optimized code (and then what happens)?

Anyway, if it is always AssertionError, I'd like to get the docs updated 
to say that.

Also...if possible, it'd be nice to have some way to ask if a thread has 
ever been started. More generally, it'd be nice to be able to determine 
what state a thread is in. I'm not sure if there are any states other 
than ready to run, running and finished. If that's it, perhaps one could 
add methods isReady and didRun (a getState method would probably be 
better if there are more states, but then one has to deal with magic 
constants).

I realize it is a common Python paradigm to assume something will work 
(such as starting the thread) and catch the exception if it doesn't, but:
- this is tricky if the exception is not documented
- sometimes one simply wants to know, without actually starting the 
thread

-- Russell




More information about the Python-Dev mailing list