Referencing other modules in atexit

Posted by Dmitry Risenberg on Stack Overflow See other posts from Stack Overflow or by Dmitry Risenberg
Published on 2010-04-03T18:11:48Z Indexed on 2010/04/03 18:23 UTC
Read the original article Hit count: 216

Filed under:
|

I have a function that is responsible for killing a child process when the program ends:

class MySingleton:
    def __init__(self):
        import atexit
        atexit.register(self.stop)

    def stop(self):
        os.kill(self.sel_server_pid, signal.SIGTERM)

However I get an error message when this function is called:

Traceback (most recent call last):
File "/usr/lib/python2.5/atexit.py", line 24, in _run_exitfuncs
   func(*targs, **kargs)
File "/home/commando/Development/Diploma/streaminatr/stream/selenium_tests.py", line 66, in stop
   os.kill(self.sel_server_pid, signal.SIGTERM)
AttributeError: 'NoneType' object has no attribute 'kill'

Looks like the os and signal modules get unloaded before atexit is called. Re-importing them solves the problem, but this behaviour seems weird to me - these modules are imported before I register my handler, so why are they unloaded before my own exit handler runs?

© Stack Overflow or respective owner

Related posts about python

Related posts about atexit