V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
gamexg
V2EX  ›  Python

python 子线程 sys.exit 无法结束进程 ... 除了 os._exit 还有什么办法优雅的结束进程吗?

  •  
  •   gamexg · 2015-09-10 15:04:52 +08:00 · 3601 次点击
    这是一个创建于 3163 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求是: python 主进程做一些工作,另起一个子线程做一些监控,发现系统异常时终止整个进程的工作,并设置进程退出代码。

    结果发现子线程调用 sys.exit 居然不会终止进程,虽然文档上面写 sys.exit 只是引发异常,但 threading.Thread 把 SystemExit 捕获并忽略了太奇怪了吧?

    os._exit 不执行各种清理操作,除了 os._exit 还有其他优雅的终止进程的办法吗?

    1 条回复    2015-09-10 19:44:20 +08:00
    ryd994
        1
    ryd994  
       2015-09-10 19:44:20 +08:00 via Android   ❤️ 1
    子线程退出了啊,主线程没退出啊
    你这样:开线程的时候指定 deamon_thread=True
    想退出的时候调用 exit_main
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1290 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 23:06 · PVG 07:06 · LAX 16:06 · JFK 19:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.