Created
June 7, 2022 21:03
-
-
Save zzzeek/d608913fce4d14e924553c91f4c6ecc0 to your computer and use it in GitHub Desktop.
list(dict.items()) atomic or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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