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 generates,loads and runs other python scripts (by calling python script.py from console).I don't have GUI and user can interact via console.I want my main program be able to respond to user pause/resume command for the running script.Should I define a thread? Whats the approach ?
Edit/Update :
Let's say I have a small python application with frontend which has various functions. I have a RUN command which runs python scripts in background .I want to implement a PAUSE feature which would pause the running python script . When the user commands RUN again then the python script should resume running . using raw_input() or print() forces user to issue command.But in this case, we don't know when user want to interrupt/pause/issue a command.So usual input/print is not usable.
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)?
所有评论(0)