Re: sys.stderr and thread



Michele Petrazzo wrote:
I have a wxpython application (the main program) and a lot of external
modules (import mymodule) that use always 2 thread (one is my
application and one is twisted with new threadselectreactor).
Sometime when I close my app, I receive a thread error that I want to
redirect to a file (I don't know why this error occur, but this is
another problem...).

This looks like it must be an exception in a *daemon* thread that is still running even as the Python interpreter dismantles itself at application exit.


This is the exception:

Unhandled exception in thread started by <bound method
Thread.__bootstrap of <Thread(Thread-1, stopped daemon)>>
Traceback (most recent call last):
  File "C:\Python23\lib\threading.py", line 451, in __bootstrap
    self.__stop()
  File "C:\Python23\lib\threading.py", line 460, in __stop
    self.__block.notifyAll()
  File "C:\Python23\lib\threading.py", line 256, in notifyAll
    self.notify(len(self.__waiters))
  File "C:\Python23\lib\threading.py", line 238, in notify
    currentThread() # for side-effect
TypeError: 'NoneType' object is not callable

One of the steps the interpreter takes is to go through all modules and rebind all globals to None. I suspect currentThread is a global in the above (although I thought that particular issue was fixed in Python 2.4... are you running an older version?).


The "simplest" thing to do is to ignore this exception because it's spurious". One way to ignore it is simply to wrap that particular daemon thread's "run" method with a "try/except: pass" so that all exceptions are swallowed quietly. Often that's not preferable, however, since it will of course swallow real exceptions too.

The "safest" thing to do is to terminate that daemon thread before the application exits. That basically means setting it to be a daemon thread is sort of pointless, but there's not really a much better solution.

A google search for "python interpreter thread bind global none" or something awful like that will probably turn up some background material, likely written by Tim Peters. :-)

-Peter
.



Relevant Pages

  • Re: Working with arrays
    ... I'm using globals because i have a config file that contains all the ... Exceptions are for files that I dun wanna select. ... Btw it's a sweet solution;) just that do u think it would be possible ... to incoporate multiple source paths and each source paths has an array ...
    (comp.lang.ruby)
  • Safe to modify globals(), or not?
    ... I'm interested in introducing new variables into the environment ... of a Python interpreter or program. ... an often-repeating warning against modifying the contents of locals(). ... is there anything wrong with modifying globals() ...
    (comp.lang.python)
  • Re: aborting without killing the python interpreter
    ... It works fine for executing as a script, ... interactively in the python interpreter it kills the interpreter. ... Exceptions do *exactly* what you want in a very clean and simple way. ...
    (comp.lang.python)