I have a question about pausing / resuming a long running program. I'll use python in this example but it could be for any programming language really.
Let's say I want to sum up all the numbers to a billion for example
results = 
for i in range(1000000000):
results.append(i + i*2)
with open('./sum.txt', 'w') as outfile:
for r in results:
Can you please specify what your actual question is?
Creating safe-points inside the loop is the way to go if you want to
preserve the loop's state. Whether file I/O is safe against corruption
doesn't depend on your application so much as operating/file system.
In Linux, it's safe to write to a temporary file, then rename it;
files are also closed automatically when a program exits.
A possible solution would be in the script, periodically checking for a "trigger" file to exist. In the example below, the loop checks once in hundred cycles, but you could make it 500 for example.
If and as long as the file exists, the loop will hold, checking again every two seconds.
While this works fine, you will have to accept the fact that it slightly slows down the loop, depending on
n; larger n will decrease the effect. ANY solution inside the loop will however have at least some effect on the loop.
Although this is on
python, the concept should be possible in any language, and on any os.
import os import time results =  # check every n- cycles for the triggere file to exist n = 100 for i in range(1000000000): if i%n == 0: # checking for the trigger file to exist while os.path.exists("test123"): time.sleep(2) print("wait") else: print("go on") else: results.append(i + i*2) print(i) with open('./sum.txt', 'w') as outfile: for r in results: output.write(r+'\n') outfile.close()
The result, if I create the trigger file and remove it again:
4096 4097 4098 4099 wait wait wait wait wait wait go on 4100 4101 4102