Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save otamajakusi/68b4165ebbb27bd75d138439b55e79d0 to your computer and use it in GitHub Desktop.
Save otamajakusi/68b4165ebbb27bd75d138439b55e79d0 to your computer and use it in GitHub Desktop.
RuntimeError: dictionary changed size during iteration
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