[Python-Dev] Weak Dictionary Iteration Behavior in Python 3

Armin Ronacher armin.ronacher at active-4.com
Sun Sep 14 11:35:52 CEST 2008


Hi,

Josiah Carlson <josiah.carlson <at> gmail.com> writes:

> i = list(d.keys())
Obviously that doesn't solve the problem.  list() consumes the generator
one after another, objects can still die when the list is created.  Imagine
the following example which uses threads::

    from time import sleep
    from weakref import WeakKeyDictionary
    from threading import Thread

    class Foo(object):
        pass

    d = WeakKeyDictionary()
    l = []

    for x in range(100000):
        obj = Foo()
        d[obj] = None
        l.append(obj)
    del obj


    def iterater():
        for item in list(d.keys()):
            pass

    Thread(target=iterater).start()

    while True:
        del l[0]

Regards,
Armin



More information about the Python-Dev mailing list