Jonathan Livni
Jonathan Livni

Reputation: 107092

debugging python's "error: can't start new thread"

Rarely I receive the following error:

Exception in thread Thread-1240:
Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 534, in __bootstrap_inner
    self.run()
  File "C:\Python26\lib\threading.py", line 738, in run
    self.function(*self.args, **self.kwargs)
  File "C:\Users\MyUser\Documents\MyProject\a_script.py", line 33, in some_func
    t.start()
  File "C:\Python26\lib\threading.py", line 476, in start
    _start_new_thread(self.__bootstrap, ())
error: can't start new thread

From here I gather I've hit some resource limit related to having too many threads in the same process. t.start() (that line 33 above) starts a Timer object which indeed opens a new thread, however, my architecture is such that no more than a few timers should exist simultaneously.

As this a rare event and I do not know how to recreate it, I would like to set it so that next time it happens I'll have all the info I need. This doesn't seem like a regular Python exception (no exception type specified...). Is it possible to try-except it? Are there alternatives to catching what's going on beyond try-catch?

Upvotes: 1

Views: 1948

Answers (1)

MRAB
MRAB

Reputation: 20654

It is a normal exception, actually it's thread.error ("thread" is the module which does all the low-level stuff). It's aliased in the "threading" module as ThreadError, so just catch threading.ThreadError.

Upvotes: 4

Related Questions