Skip to content

Instantly share code, notes, and snippets.

View TimPansino's full-sized avatar

Timothy Pansino TimPansino

View GitHub Profile
@TimPansino
TimPansino / main.py
Created November 30, 2022 23:01
Python threadsafe WeakValueDictionary and WeakKeyDictionary iteration
"""
This file explores differences in behavior with safely or unsafely iterating on weakref's dictionary types and collections.OrderedDict vs the builtin dictionary type.
Tested with CPython 2.7, CPython 3.7 - CPython 3.11.
Conclusion:
- The dict type can be threadsafe to iterate over keys, values, or items if iterating on a shallow copy (dict.copy())
or by passing the corresponding method to a list or tuple constructor (list(dict.items())).