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
kevindu
V2EX  ›  Python

Python 启动的 daemon 线程一定会随着主线程退出而退出吗

  •  
  •   kevindu · 2017-12-28 15:41:13 +08:00 · 4597 次点击
    这是一个创建于 2282 天前的主题,其中的信息可能已经有所发展或是发生改变。
    使用 threading 模块创建了个子线程,设置 daemon 为 True。子线程是死循环,里面使用了 sleep,用于发送心跳包。主线程是死循环,用于处理任务。那么启动这个程序之后,使用 ctrl+c 停止,那么子线程一定会结束吗?
    11 条回复    2017-12-28 17:46:45 +08:00
    ryd994
        1
    ryd994  
       2017-12-28 15:58:17 +08:00 via Android
    1. Ctrl C 的效果是发送 sigterm 信号,信号可能被任何一个线程捕捉到。你这样做很不稳
    2. 稳妥的做法是:使用 signal 模块,保证主线程捕捉到信号。主线程设置一个变量。子线程定时检测这个变量。主线程等所有子线程退出后再退出。
    MrIndescribable
        2
    MrIndescribable  
       2017-12-28 16:01:59 +08:00   ❤️ 1
    @ryd994 Ctrl + C 是 SIGINT
    yanzixuan
        3
    yanzixuan  
       2017-12-28 16:07:28 +08:00
    @ryd994 学习了
    ivechan
        4
    ivechan  
       2017-12-28 16:13:48 +08:00
    守护进程即使主线程退出也会存在的。
    kevindu
        5
    kevindu  
    OP
       2017-12-28 16:14:51 +08:00
    @ivechan 为什么呢?
    roricon
        6
    roricon  
       2017-12-28 16:19:25 +08:00
    @ivechan 说的守护进程, 楼主文的是 daemon threat, 不是同一个东西.

    1 楼已经给了一个稳妥的方法.
    下面是 daemon threat 的解释, daemon threat 会在主进程结束后自动结束.
    https://stackoverflow.com/a/190017
    kevindu
        7
    kevindu  
    OP
       2017-12-28 16:20:48 +08:00
    @ryd994 谢谢,但是不稳的原因是 因为 信号可能被任何一个线程捕捉到吗? 但是实际情况是,主线程程序(shell 中跑的)已经退出到 shell 了,但是子线程还可能在跑。这个排除了主线程没有捕捉到信号。

    那么,主线程退出的时候守护线程不会被退出吗?我看 threading 文档,Daemon threads are abruptly stopped at shutdown. Their resources (such as open files, database transactions, etc.) may not be released properly. If you want your threads to stop gracefully, make them non-daemonic and use a suitable signalling mechanism such as an Event
    roricon
        8
    roricon  
       2017-12-28 16:29:00 +08:00
    这里说的是进程结束时 daemon threads 会退出, 但这个线程"打开"的资源可能不会被正确释放.
    ryd994
        9
    ryd994  
       2017-12-28 16:38:31 +08:00
    @kevindu 我记忆中是会的,但是看源码不会:
    https://hg.python.org/cpython/file/tip/Lib/threading.py#l1275
    ryd994
        10
    ryd994  
       2017-12-28 16:55:54 +08:00
    @kevindu 仔细一想,主线程一退,就会调用 syscall exit,整个进程都退了,不需要一个个清理子线程。
    这也就是为什么说 Daemon threads are abruptly stopped at shutdown
    kevindu
        11
    kevindu  
    OP
       2017-12-28 17:46:45 +08:00
    @ryd994 嗯嗯,后面我采用了变量来在 ctrl+c 时候确保 daemon 子线程退出之后再退出主线程。这样两种方式确保子线程退出吧。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3492 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:59 · PVG 18:59 · LAX 03:59 · JFK 06:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.