简体   繁体   中英

Running a python script and changing git branch

I am trying to find ways to make better use of my time while programming.

I have a python script that does some heavy work (it can take hours) to finish. Now, most of the work it does is network related, so i have plenty of cpu resources to spare.

If the script was a C binary executable, it would be fine to git checkout onto a different branch and do extra work, I could even modify the binary in disk as it has been copied to ram, so until it finishes running I won't affect program output.

But python scripts are translated, not compiled. What happens if I start tampering with the source file, can i corrupt the programs output, or is the text file and associated imports copied to RAM, allowing me to tamper with the source with no risk of changing the behaviour of the running program?

In general, if you have a single Python file which you run as a script, you're fine. When you run the file, it is compiled into bytecode which is then executed. You can change the original script at this point and nothing breaks.

However, we can deliberately break it by writing some horrible but legal code like this:

horrible.py :

from time import sleep


sleep(10)
import silly
silly.thing()

silly.py :

def thing():
    print("Wow!")

You can run horrible.py and while it is running you can edit silly.py on disk to make it do something else. When silly.py is finally import ed, the updated version will be loaded.

A workaround is to put all your imports at the top of the file, which you probably do anyway.

When a python program is run it is compiled (kinda, more like translated) into a .pyc file that is then run by the python interpreter. When you change a file it should NOT affect the code if it is already running.

Here is a related stackoverflow answer. What will happen if I modify a Python script while it's running?

Why not have another working directory where you make your modifications? Is there a lot of ancillary data or something that makes it hard to set up a working directory? Ie if your working directory is A , git clone AB , and then work in B . When you're done, you can pull the changes back from B to A :

git remote add B ../B
git pull B master

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM