[Python-Dev] threading issue: preventing starting twice

Guido van Rossum guido at python.org
Wed Feb 25 16:40:49 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)?

Hey, it's open source!  You can look for yourself. :-)

Yes, the test is based on assert.

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

Disagree.  You shouldn't do that.  What happens if you ignore the
advice needn't be specified.

> 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).

There is isRunning().  Isn't that enough?  I realize it doesn't
distinguish between "not yet running" and "no longer running", but
your application state should be enough to distinguish between the
two, right?

> 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

What's your use case?  Why can't you add application-specific state
to track whatever you're interested in?

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list