Suppose that I have put a Python script to run. Let's say while it is running, I open the source code and change the value of a variable to different value. Now, in another terminal if I start running the latest source code, what happens to the previous run that is progress?
Will it be get affected because of this latest change that I did while I was running it?
The thing is that I want to do parallel runs of the program for different values of a particular variable. Any better way to do this?
Python compiles your source into bytecode and runs that bytecode. Once the source file has been read it is no longer needed to run that bytecode. Changes to the source file won't then affect already running code.
However, if an exception is raised and Python tries to format a traceback for display, it'll reach back to the source code, mapping line markers in the bytecode back to source lines. If the source file changed after compilation, this could mean the wrong lines are being displayed. That can create confusion.
You can easily give your program command line parameters to vary how it behaves. Take a look at the sys.argv
list, and perhaps the argparse
module for more complex command-line option handling. That way your code remains stable and flexible.
Python typically compiles the source code to a *.pyc file. Changing the value in the script usually won't affect the value already in memory.
The better way to do this is take an argument from argv
python your_script.py value
You can access it with
import sys
sys.argv[1] #this is the 'value' from the command line
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With