I am using file locking in python script (to control single instance of it execution).
http://code.google.com/p/pylockfile/
I release lock in finally code block.
But if script closed, for example, closing the terminal running it, the finally block doesn’t execute and the file stays locked.
How to catch python script destructor event in any case?
See this blog post regarding this subject. It uses the win32api when under Windows, while under Linux the
SIGTERMsignal is caught. To verify its working, it might be helpful to write something to a file in theon_exithandler like as done below. As the snippet is quite brief, I’ll just include it (full props to the blog author):If you will close the terminal running that program, it will create a file to verify the callback functionality.