1

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?

Community
  • 1
  • 1
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

1 Answers1

4

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.

MRAB
  • 20,356
  • 6
  • 40
  • 33
  • 1
    There's the occasional inconsistency in the standard library which remains because it would break existing code (and possibly annoy existing users) if it changed. The re module also has an exception called "error". – MRAB Jun 30 '11 at 18:52