Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94507 > unrolled thread

register cleanup handler

Started byNeal Becker <ndbecker2@gmail.com>
First post2015-07-24 10:57 -0400
Last post2015-07-24 13:35 -0400
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  register cleanup handler Neal Becker <ndbecker2@gmail.com> - 2015-07-24 10:57 -0400
    Re: register cleanup handler Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2015-07-24 18:41 +0200
      Re: register cleanup handler Neal Becker <ndbecker2@gmail.com> - 2015-07-24 13:35 -0400

#94507 — register cleanup handler

FromNeal Becker <ndbecker2@gmail.com>
Date2015-07-24 10:57 -0400
Subjectregister cleanup handler
Message-ID<mailman.950.1437749869.3674.python-list@python.org>
I have code like:

if (condition):
  do_something_needing_cleanup

code_executed_unconditionally

<cleanup has to happen here if required, even if above did return, continue 
or exception>

Now, how can I make sure cleanup happens?  Actually, what I really would 
like, is:

if (condition):
  do_something_needing_cleanup
  register_scoped_cleanup (cleanup_fnc)

code_executed_unconditionally


So, any thoughts/hopes of python being able to do something like this?

I know we have try/finally, but I don't think that helps here, because
code_executed_unconditionally couldn't be inside the try.  Or am I missing 
something obvious?

[toc] | [next] | [standalone]


#94512

FromIrmen de Jong <irmen.NOSPAM@xs4all.nl>
Date2015-07-24 18:41 +0200
Message-ID<55b26ad1$0$2920$e4fe514c@news.xs4all.nl>
In reply to#94507
On 24-7-2015 16:57, Neal Becker wrote:
> I have code like:
> 
> if (condition):
>   do_something_needing_cleanup
> 
> code_executed_unconditionally
> 
> <cleanup has to happen here if required, even if above did return, continue 
> or exception>
> 
> Now, how can I make sure cleanup happens?  Actually, what I really would 
> like, is:
> 
> if (condition):
>   do_something_needing_cleanup
>   register_scoped_cleanup (cleanup_fnc)
> 
> code_executed_unconditionally
> 
> 
> So, any thoughts/hopes of python being able to do something like this?
> 
> I know we have try/finally, but I don't think that helps here, because
> code_executed_unconditionally couldn't be inside the try.  Or am I missing 
> something obvious?
> 

Sounds like you want a context manager, see
https://docs.python.org/3/library/contextlib.html#replacing-any-use-of-try-finally-and-flag-variables

Irmen

[toc] | [prev] | [next] | [standalone]


#94514

FromNeal Becker <ndbecker2@gmail.com>
Date2015-07-24 13:35 -0400
Message-ID<mailman.954.1437759337.3674.python-list@python.org>
In reply to#94512
Irmen de Jong wrote:

> On 24-7-2015 16:57, Neal Becker wrote:
>> I have code like:
>> 
>> if (condition):
>>   do_something_needing_cleanup
>> 
>> code_executed_unconditionally
>> 
>> <cleanup has to happen here if required, even if above did return,
>> continue or exception>
>> 
>> Now, how can I make sure cleanup happens?  Actually, what I really would
>> like, is:
>> 
>> if (condition):
>>   do_something_needing_cleanup
>>   register_scoped_cleanup (cleanup_fnc)
>> 
>> code_executed_unconditionally
>> 
>> 
>> So, any thoughts/hopes of python being able to do something like this?
>> 
>> I know we have try/finally, but I don't think that helps here, because
>> code_executed_unconditionally couldn't be inside the try.  Or am I
>> missing something obvious?
>> 
> 
> Sounds like you want a context manager, see
> https://docs.python.org/3/library/contextlib.html#replacing-any-use-of-try-finally-and-flag-variables
> 
> Irmen

Yes, that looks great!  I'm using py2.7.10, so I tried 'contexter' package.
if (condition):

with ExitStack() as stack:
  if condition:
    do_something()
    def cleanup_resources():
      some_cleanup_using_closure()
    stack.callback(cleanup_resources)
  unconditionally_executed_code_no_worrying_about_cleanup()

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web