简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM