Skip to content

Instantly share code, notes, and snippets.

@zzzeek
Created June 7, 2022 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzzeek/d608913fce4d14e924553c91f4c6ecc0 to your computer and use it in GitHub Desktop.
Save zzzeek/d608913fce4d14e924553c91f4c6ecc0 to your computer and use it in GitHub Desktop.
list(dict.items()) atomic or not
import threading
def one(obj):
while True:
skip = set()
try:
{k: v for k, v in obj.__dict__.items() if k not in skip}
except RuntimeError as err:
print("%s" % err)
def one_proposed_protected(obj):
while True:
skip = set()
try:
{k: v for k, v in list(obj.__dict__.items()) if k not in skip}
except RuntimeError as err:
print("%s" % err)
def two(obj):
while True:
obj.foo = 'bar'
del obj.foo
class Thing:
def __init__(self):
self.__dict__.update(
{letter: value for value, letter in enumerate("abcdefghijklmnop")}
)
thing = Thing()
t2 = threading.Thread(target=two, args=(thing, ))
t2.start()
# bug in Python 3.8 - no bug in Python 3.10 (dont have 3.9 installed here)
# interestingly, doesn't raise in py2 either
t1 = threading.Thread(target=one, args=(thing, ))
# seems to resolve
# t1 = threading.Thread(target=one_proposed_protected, args=(thing, ))
t1.start()
t1.join()
t2.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment