Python线程内存

2024-10-01 00:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的课程:

class Detector:
    ...
    def detect:
        sniff(iface='eth6', filter='vlan or not vlan and udp port 53', prn=self.spawnThread, store=0)

    def spawnThread(self, pkt):
        t = threading.Thread(target=self.predict, args=(pkt,))
        t.start()

    def predict(self, pkt):
        # do something
        # write log file with logging module

其中sniff()是来自scapy的方法,对于它捕获的每个包,它将该包传递给spawnThread,在spawnThread中,我想创建不同的线程来运行predict方法。在

但似乎有内存泄漏,我与Heapy进行了检查,得到以下输出:

^{pr2}$

元组对象的数量和大小不断增长,我认为这是导致内存泄漏的原因,但我不知道在哪里以及为什么。谢谢你的反馈!在

更新:如果我不使用线程直接从sniff调用predict,则不会发生内存泄漏。而且,类中的其他地方也没有其他元组对象。在__init__中,我刚刚启动了一些字符串,比如路径和名称。在

class Detector:
    ...
    def detect(self):
        sniff(iface='eth6', filter='vlan or not vlan and udp port 53', 
            prn=self.predict, store=0)

    def predict(self, pkt):
        # do something with pkt
        # write log file with loggin module

Tags: 内存selfdefwithfilterdetectorpredictclass