0

I have a python program running a resource-intensive simulation. I would like to have it capture a keypress (preferably any, but just space would work too) and report status when it does.

How should I go about doing this?

  • @Carpetsmoker Thanks! It worked. If you post as answer I will accept. –  Nov 01 '14 at 18:33

1 Answers1

0

You question doesn't provide too much details, so it's hard to give a detailed answer, but in general, I would use the signal module to print the status.

BSD has SIGINFO (bound to ^T) for exactly this purpose; Linux & Windows unfortunately don't implement this, so it's not very portable.
On Linux, but you can use SIGUSR1, you can't send this from the keyboard, but you can use kill.

You could also use SIGINT (^C) to print the status. This has the side-effect of no longer being able to quit the program with ^C, though.

Also see: Can I make use of an interrupt to print a status while still continue process?

Community
  • 1
  • 1
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146