关闭Python线程以防止内存泄漏 [英] Close Python Thread To Prevent Memory Leak

查看:595
本文介绍了关闭Python线程以防止内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to I close a Python thread in order to make sure that everything in memory within a thread is cleared from memory? Currently, I have a list of threads that I join in the following manner:

for t in threadlist:
    t.join(5)

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

These threads were originally created looping over a list of arguments to pass to each thread with myfunc which appends results to a list:

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

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

When I look at what's in memory with pympler all manner of things in the function called by the thread remain in memory after this executes. I need it to garbage collect as it would if I called myfunc normally without threading.

推荐答案

已在评论中解决:

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线程以防止内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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