vdboor
vdboor

Reputation: 22526

In Python, can a dict with currentThread() as key cause memory leaks?

I'm debugging memory leaks in a Django application, and could something curious in django_cachepurge:

from threading import currentThread

_urls_to_purge = {}

def add_purge_url(url):
    # ....

    _urls_to_purge.setdefault(currentThread(), set()).add(url)

Is such construct prone to memory leaks? I suspect so, unless I'm not familiar with some Python magic here. There is no location where the dict is cleaned up.

Upvotes: 2

Views: 334

Answers (1)

Tobias
Tobias

Reputation: 4292

I don't know what currentThread returns, but you probably can use the built-in id or hash functions on it to get a safe value.

If lookup isn't enough, e.g. because you want to iterate over the container, there is weakref.WeakKeyDictionary.

Upvotes: 1

Related Questions