no cleanup on TERM signal

Duncan Booth duncan.booth at invalid.invalid
Fri May 2 08:26:50 EDT 2008


Christian Heimes <lists at cheimes.de> wrote:

>> 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. 
> 

or replace it with:

 with contextlib.closing(create_resource()) as res:
     do_something()

if the object does not support the context manager protocol.



More information about the Python-list mailing list