简体   繁体   English

运行python脚本并更改git分支

[英]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. 我有一个python脚本,可以完成一些繁重的工作(可能需要几个小时)。 Now, most of the work it does is network related, so i have plenty of cpu resources to spare. 现在,它所做的大部分工作都与网络有关,所以我有足够的cpu资源。

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. 如果脚本是一个C二进制可执行文件,将gout checkout放到另一个分支上并做额外的工作就可以了,我甚至可以修改磁盘中的二进制文件,因为它已被复制到ram,所以直到它完成运行我不会影响程序输出。

But python scripts are translated, not compiled. 但python脚本是翻译的,而不是编译的。 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? 如果我开始篡改源文件会发生什么,我可以破坏程序输出,还是将文本文件和相关的导入文件复制到RAM中,这样我就可以篡改源代码而不会有改变正在运行的程序行为的风险?

In general, if you have a single Python file which you run as a script, you're fine. 一般来说,如果您有一个Python文件作为脚本运行,那么你没问题。 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 : horrible.py

from time import sleep


sleep(10)
import silly
silly.thing()

silly.py : 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. 您可以运行horrible.py并在运行时可以编辑磁盘上的silly.py以使其执行其他操作。 When silly.py is finally import ed, the updated version will be loaded. 最终import silly.py ,将加载更新的版本。

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. 当一个python程序运行时,它被compiled (有点,更像是翻译)到.pyc文件,然后由python解释器运行。 When you change a file it should NOT affect the code if it is already running. 更改文件时,如果代码已在运行,则不应影响该代码。

Here is a related stackoverflow answer. 这是一个相关的stackoverflow答案。 What will happen if I modify a Python script while it's running? 如果我在运行时修改Python脚本会发生什么?

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 . 即如果你的工作目录是Agit clone AB ,然后在B工作。 When you're done, you can pull the changes back from B to A : 完成后,您可以将更改从B拉回到A

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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