I'm having problems with quitting a Pygame application (6502 based computer emulator).

The part regarding exit looks like this:

while True:

  for event in pygame.event.get()
    if event.type == pygame.QUIT:

I'm also using pygame.mixer to play sounds as they appear, but I'm not sure that's related. I'm also using pygame.time.set_timer to update screen in regular intervals and play sound if buffer has filled enough. Is it possible the event queue overfills? How would I determine if this is the case?

It simply freezes in 40-50% cases, and attaching strace -p to the python interpreter gives:

Process 21326 attached
futex(0x7f5a32f839d0, FUTEX_WAIT, 21329, NULL

Also, I'm getting occasional pulseaudio related crashes, is this combination known to be buggy?

Any ideas and suggestions are more than welcome! Thanks!

EDIT: it freezes when I click the window's close button (x)

  • \$\begingroup\$ This is just guess, but sys.exit() only raises an exeption. If anything catches it (pygame or perhaps pygame.mixer) it won't actually exit. Try calling break or return instead to see if that's the problem. \$\endgroup\$ – Nocturno Nov 13 '15 at 18:22
while RUNNING:    # Loop while RUNNING == True

    for event in pygame.event.get()
        if event.type == pygame.QUIT:
            RUNNING = False    # Set RUNNING = False and end the loop

pygame.quit()    # Quit pygame and allow the program to end

Instead of creating an infinite loop, assign it's execution to a variable, in this case RUNNING. Then, when an exit event is raised, you can just set the variable, RUNNING, to false. That will end the loop where Pygame can be exited after the loop and allow the program to end naturally without raising any exceptions and without having to use the sys module.

  • \$\begingroup\$ I tried that, but it behaves the same... sometimes it quits normally, sometimes it hangs waiting for some thread. If You have any advice how to debug further, I'm all ears. \$\endgroup\$ – Hrvoje Nov 27 '15 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.