no cleanup on TERM signal

Christian Heimes lists at cheimes.de
Fri May 2 05:59:33 EDT 2008


Laszlo Nagy schrieb:
> For sensitive resources, instead of writing __del__ methods, you should
> create a "close()" method. Python does this with file objects, DB API
> 2.0 with database connection objects etc. Then you can do
> 
> res = create_resource()
> try:
>    use_resource()
> finally:
>    res.close() # Must free resource, but the object can still be alive...

You can replace the try/finally code with a "with resource:
do_something()" block if the object supporst the context manager protocol.

If you want to run some code during the shutdown phase of the Python
process you can register a callback function in the "atexit" module.

> It is more common to use signals when you have more threads or child
> processes. You can use something like:

Do NOT mix threads and signals. It's usually a very bad idea and may
lead to surprising side effects. Signals are only handled by the main
thread.

Christian



More information about the Python-list mailing list