python中的线程是否需要加入以避免泄漏? [英] Do threads in python need to be joined to avoid leakage?

查看:33
本文介绍了python中的线程是否需要加入以避免泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解加入线程的目的,我在询问资源使用情况.我在这里的具体用例是我有一个长时间运行的进程,它需要产生许多线程,并在操作期间检查它们是否已终止,然后将它们清理干净.主线程等待 inotify 事件并根据这些事件产生线程,因此它不能阻塞 join() 调用,因为它需要阻塞 inotify 调用.

I understand the purpose of joining a thread, I'm asking about resource use. My specific use-case here is that I have a long-running process that needs to spawn many threads, and during operation, checks if they have terminated and then cleans them up. The main thread waits on inotify events and spawns threads based on those, so it can't block on join() calls, because it needs to block on inotify calls.

我知道,例如,对于 pthreads,不加入终止的线程会导致资源泄漏:

I know that with pthreads, for instance, not joining a terminated thread will cause a resource leak:

PTHREAD_JOIN(3):与可连接的线程(即未分离的线程)连接失败,会产生僵尸线程".避免这样做,因为每个僵尸线程都会消耗一些系统资源,并且当积累了足够多的僵尸线程时,将无法再创建新的线程(或进程).

PTHREAD_JOIN(3): Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread". Avoid doing this, since each zombie thread consumes some system resources, and when enough zombie threads have accumulated, it will no longer be possible to create new threads (or processes).

Python 的文档虽然没有说明这样的事情,但它也没有指定如果许多线程在正常操作期间没有被加入而预计会​​自行结束,则可以毫无问题地忽略 join().

Python's documentation says no such thing, though, but it also doesn't specify that the join() can be disregarded without issue if many threads are expected to end on their own without being joined during normal operation.

我想知道,我可以简单地获取我的线程列表并执行以下操作:

I'm wondering, can I simply take my thread list and do the following:

threads = [thread for thread in threads if thread.is_alive()]

对于每次检查,还是会泄漏?还是我必须执行以下操作?

For each check, or will this leak? Or must I do the following?

alive_threads = list()
for thread in threads:
    if thread.is_alive():
        alive_threads.append(thread)
    else:
        thread.join()
threads = alive_threads

推荐答案

TLDR:否.Thread 自行清理底层资源.

TLDR: No. Thread cleans up the underlying resources by itself.

Thread.join 只是等待线程结束,不执行清理.基本上,每个 Thread 都有一个锁,当线程完成并随后清理时释放锁.Thread.join 只是等待锁被释放.

Thread.join merely waits for the thread to end, it does not perform cleanup. Basically, each Thread has a lock that is released when the thread is done and subsequently cleaned up. Thread.join just waits for the lock to be released.

Thread.join 做了一些小的清理工作,即移除锁并设置一个标志来将线程标记为死线程.这是一种避免不必要地等待锁的优化.然而,这些是内部的,并且也由依赖于锁和标志的所有其他公共方法执行.最后,这种清理在功能上相当于一个 Thread 由于垃圾收集而被自动清理.

There is some minor cleanup done by Thread.join, namely removing the lock and setting a flag to mark the thread as dead. This is an optimisation to avoid needlessly waiting for the lock. These are internal, however, and also performed by all other public methods relying on the lock and flag. Finally, this cleanup is functionally equivalent to a Thread being cleaned up automatically due to garbage collection.

这篇关于python中的线程是否需要加入以避免泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆