modify a running python program

≡放荡痞女 提交于 2019-12-13 12:57:24

问题


I launched a python program with many nested loops, and the program will take days. I just realized that one of the loops values is wrong and makes a infinite loop.

I don't want to restart the program from zero, is there a way to interrupt the current program and modify the loop range so it will work properly and also if it was trapped with the infinite loop to break it?

Many thanks for your help.


回答1:


If the program saves its state or its results from time to time, you could add a logic which skips the steps which have already executed.

Otherwise, I don't see a way to change this.




回答2:


I guess pretty old article but just came across now. In case you still want to try you could do the following:

Make your script run under pdb as: python -m pdb

This will run it under pdb. After entering pdb just enter command 'c' (continue). This will begin your program.

When you encounter a infinite loop just do a ctrl+c this will stop the program within the debugger. Now you could run any python statements you want. Possibly you could also define a new script import it and run functions from that script or exit.

I know it is not a good idea to always run under debugger, but at least the above would solve what you intended for.



来源:https://stackoverflow.com/questions/11186600/modify-a-running-python-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!