modify a long-running python script while it is running?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Rutt

    modify a long-running python script while it is running?

    I often execute a long-running python script which is a "driver" for
    my application; it may run for several hours on a large input.

    Under CPython, is it safe for me to modify the Python script and run
    it under some smaller inputs for test purposes, while the long-running
    script is still running? It seems safe to do so, but I want to be
    sure. I know, for example, that #!/bin/sh on my system is not safe in
    this regard.

    I suppose this question could be reformatted as "does CPython read the
    entire script file into memory and then close the file before
    executing the script, making it safe for me to modify the script after
    I know it has started?"[1]

    Thank you,

    Footnotes:
    [1] how would I know that it is started? For example, at least one
    print statement I am expecting has been executed.
    --
    Benjamin Rutt
  • Mike Meyer

    #2
    Re: modify a long-running python script while it is running?

    Benjamin Rutt <rutt@bmi.osu.e du> writes:[color=blue]
    > I suppose this question could be reformatted as "does CPython read the
    > entire script file into memory and then close the file before
    > executing the script, making it safe for me to modify the script after
    > I know it has started?"[1][/color]

    Yes, but (didn't you expect that?) it loads imported files when it
    first executes an import statement for them. Future import statements
    will just get a reference to the loaded module, and won't reload
    it. So after your application imports all the modules it needs, it
    won't refer to the copies on disk again, after which it will be safe
    for you to modify the on-disk copies.

    Of course, you really ought to be working on a *copy* of the
    application sources. When you're happy that your modifications are ok,
    copy your development sources into production.

    And finally, to force python to reload a module that it's already
    loaded, use "reload(modulen ame)".

    <mike
    --
    Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

    Comment

    • Peter Hansen

      #3
      Re: modify a long-running python script while it is running?

      Kevin Yuan wrote:[color=blue]
      > I have a silly question: Can *VERY* large script cause memory overflow
      > if python load all the entire script file into memory?[/color]

      If you have a script that is literally larger than memory, then what
      else would happen but an overflow error of some kind?

      What's the point of the question, really? You don't plan to load a
      script that is larger than, say, 100K, do you? Maybe if you can
      describe your real concern we can help better.

      -Peter

      Comment

      Working...