How to “stop” and “resume” long time running Python script?

前端 未结 3 1494
甜味超标
甜味超标 2020-12-09 20:32

I wrote Python script that processes big number of large text files and may run a lot of time. Sometimes, there is a need to stop the running script and to

3条回答
  •  生来不讨喜
    2020-12-09 21:25

    Here is something simple that hopefully can help you:

    import time
    import pickle
    
    
    REGISTRY = None
    
    
    def main(start=0):
        """Do some heavy work ..."""
    
        global REGISTRY
    
        a = start
        while 1:
            time.sleep(1)
            a += 1
            print a
            REGISTRY = pickle.dumps(a)
    
    
    if __name__ == '__main__':
        print "To stop the script execution type CTRL-C"
        while 1:
           start = pickle.loads(REGISTRY) if REGISTRY else 0
            try:
                main(start=start)
            except KeyboardInterrupt:
                resume = raw_input('If you want to continue type the letter c:')
                if resume != 'c':
                    break
    

    Example of running:

    $ python test.py
    To stop the script execution type CTRL-C
    1
    2
    3
    ^CIf you want to continue type the letter c:c
    4
    5
    6
    7
    8
    9
    ^CIf you want to continue type the letter c:
    $ python test.py
    

提交回复
热议问题