I am running multiprocessing in Python with start method ‘spawn’. I have the following code:
import time
import multiprocessing
def print_something():
print("1")
def main():
multiprocessing.set_start_method('spawn')
while True:
process = multiprocessing.Process(target=print_something)
process.start()
process.join()
time.sleep(1)
if __name__ == "__main__":
main()
When I run this piece of code, python will print ‘1’ every second. My problem is the following. I start running the main() function and let it run on the background. If I edit the above code, let’s say I change print(“1”) to print(“2”), and I save the code, the background function will start printing “2” instead of “1”. This means that saving the code is not safe: if I for example save bugged code, the background function will crash.
The multiprocessing start method influences the result. If I use ‘fork’, it will always print ‘1’ even after editing and saving the code. However, both ‘spawn’ and ‘forkserver’ uses the updated script, instead of the script at the moment of starting main().
What I want is to safely edit and save the code without it affecting background processes, while using start method ‘spawn’. How can I achieve this?