如何关闭 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/