boppreh / keyboard

Hook and simulate global keyboard events on Windows and Linux.
MIT License
3.81k stars 434 forks source link

RuntimeError: dictionary changed size during iteration #127

Open MiraToutounji opened 6 years ago

MiraToutounji commented 6 years ago

The code runs properly when I just had the if "keyboard.is_pressed('y')==1:" but when I added the break command to exit the loop when n is pressed, I received an error pointing to the "if keyboard.is_pressed('y')==1:" line.

Here's my code

while not zebraledapi.disconnected():
  if keyboard.is_pressed('y')==1:
    while True:

      #if brightnessTemp < 0.5:
      #brightnessTemp += 0.001

      if j <50:
        j +=1
      else:
        j = 0

      for n in range(LEDS_TOTAL):
        i = 3 * n
        if n == j: 
            colour = (0.25,0,0.25) # RGB

        elif n == (j+1):
            colour = (0.25,0,0.5) # RGB

        elif n == (j +2):
            colour = (0.25,0,0.75) # RGB

        elif n == (j+3):
            colour = (0.25,0,1) # RGB

        else:
            colour = (0,0,0) # RGB

        values[i + 0] = int(colour[1] * brightnessTemp * 255.5)
        values[i + 1] = int(colour[0] * brightnessTemp * 255.5)
        values[i + 2] = int(colour[2] * brightnessTemp * 255.5)

      zebraledapi.leds.write(  0, values[  0: 48])
      zebraledapi.leds.write( 48, values[ 48: 96])
      zebraledapi.leds.write( 96, values[ 96:144])
      zebraledapi.leds.write(144, values[144:192])
      time.sleep(1/LEDS_REFRSH_FRQ)

      if keyboard.is_pressed('n') == 1:
        break
boppreh commented 6 years ago

Hi @MiraToutounji

This sounds like the same problem from https://github.com/boppreh/keyboard/issues/64 . The problem is that the code is busy waiting, which is technically legal but should be avoided. That other issue has been fixed, and I'm now trying to reproduce the error in this case.

As a workaround, I suggest using "add_hotkey". Let me know if you need help converting this code to avoid busy-waiting.

I'll leave the issue open while I try to reproduce and fix the error.

boppreh commented 6 years ago

I think I've fixed the RuntimeError in the suppress branch. If somebody could test, I'd be very grateful.

MiraToutounji commented 6 years ago

Thanks for the update! I'm still getting the same error.

boppreh commented 6 years ago

):

boppreh commented 6 years ago

Issue #64 has been fixed, and I can't reproduce your issue. Could you include a minimal test case (without the zebra library), or at least a stack trace to help debug the issue?

Also note that in the meantime the fix has been released in v0.13.1.

Moondra commented 5 years ago

Error seems to have been fixed when upgrading to the latest version for a script I was running.