Created
January 31, 2022 04:06
-
-
Save otamajakusi/68b4165ebbb27bd75d138439b55e79d0 to your computer and use it in GitHub Desktop.
RuntimeError: dictionary changed size during iteration
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
from threading import Thread | |
from time import sleep | |
mydict = {} | |
def x(): | |
while True: | |
# RuntimeError: dictionary changed size during iteration | |
for k in mydict.keys(): | |
print(k) | |
sleep(2) | |
def y(): | |
while True: | |
for i in range(1000): | |
mydict[i] = i | |
sleep(1) | |
t1 = Thread(target=x) | |
t2 = Thread(target=y) | |
t1.start() | |
t2.start() | |
t2.join() | |
t1.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment