推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
dwjgwsm
V2EX  ›  Python

请问如何在手动停止 py 文件的时候做一些善后工作?

  •  
  •   dwjgwsm · Apr 20, 2018 · 4501 views
    This topic created in 3127 days ago, the information mentioned may be changed or developed.
    比如手动停止 py 文件运行时自动保存一些变量什么的. 找来找去也没找到合适的方法.
    12 replies  •  2018-04-24 15:47:12 +08:00
    wwqgtxx
    wwqgtxx
    Apr 20, 2018 via iPhone
    1
    一般都是用信号量去通知进程自己退出
    Sylv
    Sylv
    Apr 20, 2018 via iPhone❤️1
    2
    except KeyboardInterrupt
    byteli
    byteli
    Apr 20, 2018
    3
    关键词 python 信号处理 SIGINT
    Aliencn
    Aliencn
    Apr 20, 2018
    4
    数据量不大的话可以变量更改时在其他地方也写一份,比如用 log,redis 之类的
    SingeeKing
    SingeeKing
    Apr 20, 2018
    5
    except KeyboardInterrupt:
    pass
    Pyjamas
    Pyjamas
    Apr 20, 2018
    6
    ```
    import atexit
    def exit_handler():
    global config
    with open('reservoir_simulate.config', 'w') as config_file:
    config_file.write(json.dumps(config, indent = 4))
    print('Program finished! Current node = ' +
    str(config["reservoir"]["start_node"]))
    if __name__ == '__main__':
    atexit.register(exit_handler)
    ```
    [Doing something before program exit]( https://stackoverflow.com/questions/3850261/doing-something-before-program-exit)
    Pyjamas
    Pyjamas
    Apr 20, 2018
    7
    Sorry 打错了- -
    ```
    import atexit
    def exit_handler():
    ...
    if __name__ == '__main__':
    atexit.register(exit_handler)
    ```
    dwjgwsm
    dwjgwsm
    OP
    Apr 21, 2018
    8
    try...except 和 atexit 都不行,singal 倒是可以终止程序,但是不能注册函数,如果注册函数,则无法终止程序,处理函数也不会执行:
    #main.py
    ..........

    def hander(a,b):

    print('.....test........')

    if __name__ == '__main__':

    import signal

    signal.signal(signal.SIGTERM,hander) #如果注册函数则无法终止程序,不注册则可以终止程序

    from PyQt5.QtCore import QCoreApplication

    app = QCoreApplication(sys.argv)

    main = MainEngine()

    main.login()

    app.exec_()


    #a.py

    import signal

    import os

    os.kill(<pid>,signal.SIGTERM)
    guyskk0x0
    guyskk0x0
    Apr 22, 2018 via Android
    9
    @dwjgwsm signal hander 里要主动调用 sys.exit 退出。kill 信号有很多种,Unix 信号机制了解一下。
    Pyjamas
    Pyjamas
    Apr 23, 2018
    10
    atexit 为啥不行,我平时都用这个,就是 ctrl+c 或者 kill -INT $pid 杀死进程的时候,exit_handler()函数都会执行的
    cortexm3
    cortexm3
    Apr 24, 2018
    11
    信号量
    luoqize1
    luoqize1
    Apr 24, 2018
    12
    捕获 ctrl+c
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4339 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 08:34 · PVG 16:34 · LAX 00:34 · JFK 03:34
    ♥ Do have faith in what you're doing.