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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • use dmgass at hotmail dot com

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

    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!!
  • Miki Tebeka

    #2
    Re: How do you register cleanup code to be run after script execution?

    Hello,
    [color=blue]
    > 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.[/color]
    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 ---[color=blue][color=green][color=darkred]
    >>> import hello[/color][/color][/color]
    hello[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]


    HTH.
    Miki

    Comment

    • Ben Finney

      #3
      Re: How do you register cleanup code to be run after script execution?

      On 08 May 2004 13:09:36 GMT, use dmgass at hotmail dot com wrote:[color=blue]
      > 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.[/color]

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

      Comment

      • Peter Otten

        #4
        Re: How do you register cleanup code to be run after script execution?

        use dmgass at hotmail dot com wrote:
        [color=blue]
        > 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.[/color]

        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

        Comment

        • pxlpluker

          #5
          Re: How do you register cleanup code to be run afterscript execution?

          why not use
          try:
          code
          finally:
          cleanup code


          Peter Otten wrote:
          [color=blue]
          >use dmgass at hotmail dot com wrote:
          >
          >
          >[color=green]
          >>I'm writing a module and when it is imported by a script I want some code
          >>automatical ly 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.
          >>
          >>[/color]
          >
          >I think you want atexit.register ():
          >
          ><importing.p y>
          >print "in main"
          >import imported
          >raw_input("typ e enter to finish")
          ></importing.py>
          >
          ><imported.py >
          >import atexit
          >
          >def exitFunc(*args) :
          > print "exitFunc callled with", args
          >
          >atexit.registe r(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
          >
          >
          >[/color]

          Comment

          • Peter Otten

            #6
            Re: How do you register cleanup code to be run after script execution?

            pxlpluker wrote:
            [color=blue]
            > why not use
            > try:
            > code
            > finally:
            > cleanup code[/color]

            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 registeredFunct ions:
            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 (cleanupForModu le) into
            every module needing cleanup doesn't affect its public interface and thus
            simplifies client code.

            Peter

            Comment

            • Dan Gass

              #7
              Re: How do you register cleanup code to be run after script execution?

              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

              Comment

              Working...