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

B

Benjamin Rutt

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.
 
M

Mike Meyer

Benjamin Rutt said:
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]

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(modulename)".

<mike
 
P

Peter Hansen

Kevin said:
I have a silly question: Can *VERY* large script cause memory overflow
if python load all the entire script file into memory?

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top