Finish loop then quit if 'q' key pressed in python

Viewed 43

I am creating a genetic algorithm in python, and I want it to continue creating generations until queued to stop. I want it to stop smoothly, but also alert the user that the stopping key was pressed.

Each generation takes about a minute to run on my pc, so ideally what would happen is the user would press 'q', then the code would print something like 'q key pressed, finishing generation then stopping'

then once the loop was completed it would break, and I could handle options like plotting,saving,continuing etc.)

Does anyone know a way I can accomplish this to be actively listening for key press so that it immediately prints message, but then continues minute long loop before breaking?

ex) output

  • Gen1 done
  • Gen2 done
  • Gen3 done
  • q key pressed, finishing generation then stopping
  • Gen4 done
  • Stopping
0 Answers
Related