Source From Here
Question
I have a module that uses ctypes to wrap some functionality from a static library into a class. When the module loads, it calls an initialize function in the static library. When the module is unloaded (presumably when the interpreter exits), there's an unload function in the library that I'd like to be called. How can I create this hook?
HowTo
Use the atexit module:
Another simple example from the docs, using register as a decorator:
I have a module that uses ctypes to wrap some functionality from a static library into a class. When the module loads, it calls an initialize function in the static library. When the module is unloaded (presumably when the interpreter exits), there's an unload function in the library that I'd like to be called. How can I create this hook?
HowTo
Use the atexit module:
- import mymodule
- import atexit
- # call mymodule.unload('param1', 'param2') when the interpreter exits:
- atexit.register(mymodule.unload, 'param1', 'param2')
- import atexit
- @atexit.register
- def goodbye():
- print "You are now leaving the Python sector."
沒有留言:
張貼留言