2021年7月3日 星期六

[ Python 常見問題 ] Detect when a Python module unloads

 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:
  1. import mymodule  
  2. import atexit  
  3.   
  4. # call mymodule.unload('param1''param2') when the interpreter exits:  
  5. atexit.register(mymodule.unload, 'param1''param2')  
Another simple example from the docs, using register as a decorator:
  1. import atexit  
  2.   
  3. @atexit.register  
  4. def goodbye():  
  5.     print "You are now leaving the Python sector."  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...