How do you register cleanup code to be run after script execution?

  • Thread starter use dmgass at hotmail dot com
  • Start date
U

use dmgass at hotmail dot com

I'm writing a module and when it is imported by a script I want some code
automatically executed when the importing script is finished executing. I'd
like it to execute before interactive mode is entered when executing the
importing script from the command line. I don't want to have to impose that
the importing script must call a function at it's end.

Any help is greatly appreciated!!
 
B

Ben Finney

I'm writing a module and when it is imported by a script I want some
code automatically executed when the importing script is finished
executing.

I'm not aware of any way to do what you want at the module level. What
are you trying to accomplish that isn't satisfied by having objects do
their cleanup themselves, in __del__() functions?
 
M

Miki Tebeka

Hello,
I'm writing a module and when it is imported by a script I want some code
automatically executed when the importing script is finished executing. I'd
like it to execute before interactive mode is entered when executing the
importing script from the command line. I don't want to have to impose that
the importing script must call a function at it's end.
If I understand you correctly than you can just add a function call at
the end of the module. This function will be executed once when the
module is imported.

--- hello.py --
a = 1
b = 2
def hello():
print "Hello"
c = 3

hello()
--- hello.py ---

HTH.
Miki
 
P

Peter Otten

use said:
I'm writing a module and when it is imported by a script I want some code
automatically executed when the importing script is finished executing.
I'd like it to execute before interactive mode is entered when executing
the
importing script from the command line. I don't want to have to impose
that the importing script must call a function at it's end.

I think you want atexit.register():

<importing.py>
print "in main"
import imported
raw_input("type enter to finish")
</importing.py>

<imported.py>
import atexit

def exitFunc(*args):
print "exitFunc callled with", args

atexit.register(exitFunc, "with", "args")

print "imported 'imported'"
</imported.py>

$ python importing.py
in main
imported 'imported'
type enter to finish
exitFunc callled with ('with', 'args')
$

Peter
 
P

Peter Otten

pxlpluker said:
why not use
try:
code
finally:
cleanup code

Because cleanup code will run immediately after code (regardless of an
exception that might occur in code) whereas atexit.register(cleanupCode)
will trigger the cleanupCode() function when the script terminates. You can
think of atexit as

try:
whole script
finally:
for f in registeredFunctions:
f()

You could implement this manually as you have suggested, but then you'd need
to know what cleanup code is necessary for every module you imported. If
you have modules needing cleanup that aren't always imported, you'd even
have to check for these. Putting atexit.register(cleanupForModule) into
every module needing cleanup doesn't affect its public interface and thus
simplifies client code.

Peter
 
D

Dan Gass

Upon reflection, this is close to what I want but not quite what I
need.

I wanted this answer to use in an open source test generator framework
I am writing to unit/integration/function test Python, C, or C++ (it
could be extended to other languages). For now the user interface is
Python (someday maybe I'll make an XML interface). The user writes a
script to setup the test and the framework generates the test and
executes it.

I wanted the generation of the test and the execution of it to occur
when the script is done running. The trouble with using the atexit
mechanism is that it is uses a last in first executed. I may run into
trouble with integration tests where other things get registered after
me and that cleanup will get done before I start (and they may shut
down resources the test needs to execute). I'm thinking it maybe just
best to provide the user a function to generate/execute the test that
they must call last.

BTW, as part of this effort I required a much more powerful
configuration mechanism that is provided by the global module
ConfigParser. If you are interested in this area please see
http://config-py.sourceforge.net/.

Thanks for your responses,
Dan Gass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top