如何“停止”和“恢复”长时间运行Python脚本?

发布于 2024-11-14 21:54:46 字数 345 浏览 1 评论 0原文

我编写了处理大量大型文本文件的 Python 脚本,并且可能运行很多时间。有时,需要停止正在运行的脚本并稍后恢复。停止脚本的可能原因是程序崩溃、磁盘“空间不足”情况或许多其他必须执行的情况。我想为脚本实现一种“停止/恢复”机制。

  • 停止时:脚本退出并退出保存其当前状态。
  • 恢复:脚本启动,但从最新保存的状态继续,

我将使用picklesignal模块来实现它。

我很高兴听到如何以 pythonic 方式做到这一点。

谢谢你!

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 resume it later. The possible reasons to stop the script are program crash, disk 'out of space' situation or many others when you have to do it. I want to implement kind of "stop/resume" mechanism for the script.

  • On stop: the script quits & saves its current state.
  • On resume: the script starts, but continues from the latest saved state

I'm going to implement it using the pickle and the signal modules.

I'll be glad to hear how to do it in pythonic way.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱咩 2024-11-21 21:54:46

这是一些简单的事情,希望可以帮助您:

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

运行示例:

$ 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

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
遮了一弯 2024-11-21 21:54:46

如果您想读取大文件,只需使用文件句柄,一次读取一行,根据需要处理每一行。如果您想保存 python 会话,那么只需使用 dill.dump_session 即可——它将保存所有现有对象。其他答案将失败,因为 pickle 无法腌制文件句柄。然而,dill 可以序列化几乎所有 Python 对象——包括文件句柄。

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> f = open('bigfile1.dat', 'r')
>>> data = f.readline()  
>>> 
>>> dill.dump_session('session.pkl')
>>> 

然后退出 python 会话,然后重新启动。当您 load_session 时,您会加载 dump_session 调用时存在的所有对象。

dude@hilbert>$ python
Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> dill.load_session('session.pkl')
>>> len(data)
9
>>> data += f.readline()
>>> f.close()
>>> 

就这么简单。

在此处获取 dillhttps://github.com/uqfoundation

If you are looking to read big files, just use a file handle, and read the lines one at a time, processing each line as you need to. If you'd like to save the python session, then just use dill.dump_session -- and it will save all existing objects. Other answers will fail as pickle cannot pickle a file handle. dill, however, can serialize almost every python object -- including a file handle.

Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> f = open('bigfile1.dat', 'r')
>>> data = f.readline()  
>>> 
>>> dill.dump_session('session.pkl')
>>> 

Then quit the python session, and restart. When you load_session, you load all the objects that existed at the time of the dump_session call.

dude@hilbert>$ python
Python 2.7.9 (default, Dec 11 2014, 01:21:43) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dill
>>> dill.load_session('session.pkl')
>>> len(data)
9
>>> data += f.readline()
>>> f.close()
>>> 

Simple as that.

Get dill here: https://github.com/uqfoundation

空袭的梦i 2024-11-21 21:54:46

The execution could sleep it's life away, or (aside from the exceptions of security), the state of the script can be pickled, zipped, and stored.

http://docs.python.org/library/pickle.html

http://docs.python.org/library/marshal.html

http://docs.python.org/library/stdtypes.html (5.9)

http://docs.python.org/library/archiving.html

http://www.henrysmac.org/?p=531

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文