Can I have a running python script(under Windows)being paused in the middle by user , and resume again when user decides ?
There is a main manager program which gen
In Windows, you can suspend/resume running Python scripts . Type resmon on CMD or via Run command (Windows+R). Locate your Python script process and right-click>Suspend Process. This will unlock CPU usage but not RAM. ;)
Have you tried the obvious and print a prompt then read a line from stdin? That will pause your whole script.
What you asked in your original question isn't very clear, so if this doesn't do what you want, can you explain why?
Ok, from what I've seen in my searches on this, even with threading, sys.stdin
is going to work against you, no matter how you get to it (input()
, or even sys.stdin.read()
, .readline()
, etc.), because they block.
Instead, write your manager program as a socket server or something similar.
Write the scripts as generators, which are designed to pause execution (every time it hits a yield
), and just call next()
on each one in turn, repeatedly. You'll get a StopIteration
exception when a script completes.
For handling the commands, write a second script that connects to the manager program's socket and sends it messages, this will be the console interface the user interacts with (later, you could even upgrade it to a GUI without altering much elsewhere).
The server picks these commands up before running the next iteration on the scripts, and if a script is paused by the user, the manager program simply doesn't call next()
on that script until the user tells it to run again.
I haven't tested this, but I think it'll work better than making threads or subprocesses for the external scripts, and then trying to pause (and later kill) them.
This is really out of my depth, but perhaps running the scripts in the background and using kill -stop
and kill -cont
to pause and continue will work (assuming Linux)?