By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!!
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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 ---
import hello hello

HTH.
Miki
Jul 18 '05 #2

P: n/a
On 08 May 2004 13:09:36 GMT, use dmgass at hotmail dot com wrote:
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?

--
\ "We must become the change we want to see." -- Mahatma Gandhi |
`\ |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3

P: n/a
use dmgass at hotmail dot com wrote:
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

Jul 18 '05 #4

P: n/a
why not use
try:
code
finally:
cleanup code
Peter Otten wrote:
use dmgass at hotmail dot com wrote:
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


Jul 18 '05 #5

P: n/a
pxlpluker wrote:
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

Jul 18 '05 #6

P: n/a
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
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.