pause/resume a python script in middle

后端 未结 9 1489
再見小時候
再見小時候 2020-12-28 16:38

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

9条回答
  •  轮回少年
    2020-12-28 17:15

    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

    if os.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.

提交回复
热议问题