python - 关闭Python线程以防止内存泄漏

标签 python multithreading

如何关闭 Python 线程以确保线程内内存中的所有内容都从内存中清除?目前,我有一个通过以下方式加入的线程列表:

for t in threadlist:
    t.join(5)

这些线程最初是通过循环参数列表创建的,并使用 myfunc 将结果附加到列表中传递给每个线程:

threadlist = []
for r in arglist:
    t = Thread(target=myfunc, args=(r,))
    t.daemon = True
    threadlist.append(t)

当我使用 pympler 查看内存中的内容时,线程调用的函数中的所有内容在执行后都保留在内存中。我需要它来进行垃圾收集,就像我在没有线程的情况下正常调用 myfunc 一样。

最佳答案

Resolved in comments:

The objects being deleted were contained within an object function.
Moving them outside of that object function and
into a regular function allowed garbage collecting to occur following delete.
Michael

关于python - 关闭Python线程以防止内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44403132/

相关文章:

c++ - 多线程无法按预期工作

vb.net - 如何在单独的线程上运行 VB 项目中的表单,同时仍能够进行通信

python - 添加多个约束到 scipy 最小化,自动生成约束字典列表?

Python 相当于基于 Perl 函数的 cgi 风格

python - 使用pandas,如何加速需要使用前几行计算值的迭代?

c++ - 为每个线程重定向标准输出/标准错误

java - wait后线程恢复(if-else的作用)

python - 使用 python 为 django 设置虚拟环境的重要性

python - 如何在Python Django的设置中启用每日新的日志文件名?

java - wait() 问题;