Skip to content Skip to sidebar Skip to footer

Pause/resume A Python Script In Middle

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 an

Solution 1:

If it were unix I'd recommend signal, but here is a crude version that does what you ask.

import time

whileTrue:
    try:
        time.sleep(1)  # do something hereprint'.',

    except KeyboardInterrupt:
        print'\nPausing...  (Hit ENTER to continue, type quit to exit.)'try:
            response = raw_input()
            if response == 'quit':
                breakprint'Resuming...'except KeyboardInterrupt:
            print'Resuming...'continue

Use Ctrl+C to pause, and ENTER to resume. Ctrl+Break can probably be used as a harsh kill, but I don't have the key on this keyboard.

A more robust version could use select on a pipe/socket, or even threads.

Solution 2:

You can make a simple workaround by creating a PAUSEFILE. Your to-be-paused script may periodically check for existence (or content) of such file.

User's PAUSE command can create (or fill with proper content) such file.

I have used this approach in a similar situation, where I wanted to be able to pause my Python scripts and resume them later. They contain something like

ifos.path.isfile(PAUSEFILE):
  raw_input('Remove ' + PAUSEFILE + ' and hit ENTER to continue')

in their main loops.

It is nasty and could be broken if the code really depended on it, but for the use cases, where the pause is done by users at random, I guess it will not matter.

The PAUSE command is simply touch $PAUSEFILE.

Solution 3:

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)?

Solution 4:

I don't understand very well your approach but every time a user needs to press a enter to continue the script you should use:

input() #forpython3kraw_input() #forpython2k

without assigning the receiving answer to a variable.

Solution 5:

I found so hacky those responses, while being interesting too.

The best approach is the https://stackoverflow.com/a/7184165/2480481 doing it using KeyboardInterrupt exception.

As i noticed nobody mention "using a debugger", i'll do it.

Install pdb, Python debugger with pip install pdb. Follow that to make your script pausable https://stackoverflow.com/a/39478157/2480481 by Ctrl+c instead of exit it.

The main benefit of using a debugger (pdb) is that you can inspect the variables, values, etc. This is far way more powerfull than just pause/continue it.

Also, you can add Ipython interface with pdb attached to debug your app when crashes. Look at: https://stackoverflow.com/a/14881323/2480481

Post a Comment for "Pause/resume A Python Script In Middle"