views:

111

answers:

5

Is there a way to send an interrupt to a python module when the user inputs something in the console? For example, if I'm running an infinite while loop, i can surround it with a try/except for KeyboardInterrupt and then do what I need to do in the except block.

Is there a way to duplicate this functionality with any arbitrary input? Either a control sequence or a standard character?

Edit: Sorry, this is on linux

+1  A: 

KeyboardInterrupt is special in that it can be trapped (i.e. SIGINT under operating systems with respective POSIX support, SetConsoleCtrlHandler on Windows) and handled accordingly. If you want to process user input while at the same time doing work in a otherwise blocking loop, have a look at the threading or subprocess modules, taking a look at how to exchange data between two different threads / processes. Also be sure to get an understanding of the issues that go hand in hand with parallel computing (i.e. race conditions, thread safety / synchronization etc.)

Jim Brissom
Yeah I'm familiar with threading and all that, I'm just working on a test function right now and I was hoping there would be a simple way to inject user input into the loop other than just ^C
Falmarri
Let it be known that interacting with users is **never** simple!
jathanism
+1  A: 

I'm not sure if it's the most optimal solution but you can create a thread that does a while True: sys.stdin.read(1)

That way you can always read all input, but it will be slow and you'll have to combine the strings yourself.

Example:

import os
import sys
import time
import threading

class StdinReader(threading.Thread):
    def run(self):
        while True:
            print repr(sys.stdin.read(1))

os.system('stty raw')

stdin_reader = StdinReader()
stdin_reader.start()

while True:
    time.sleep(1)

os.system('stty sane')

The stty raw thing depends on where you're running it though. It won't work everywhere.

WoLpH
+2  A: 

You need a separate process (or possibly a thread) to read the terminal and send it to the process of interest via some form of inter-process communication (IPC) (inter-thread communication may be harder -- basically the only thing you do is send a KeyboardInterrupt from a secondary thread to the main thread). Since you say "I was hoping there would be a simple way to inject user input into the loop other than just ^C", I'm sad to disappoint you, but that's the truth: there are ways to do what you request, but simple they aren't.

Alex Martelli
+1  A: 

Dependent on the operating system and the libraries available, there are different ways of achieving that. This answer provides a few of them.

Here is the Linux/OS X part copied from there, with in infinite loop terminated using the escape character. For the Windows solution you can check the answer itself.

import sys
import select
import tty
import termios

from curses import ascii

def isData():
    return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

old_settings = termios.tcgetattr(sys.stdin)
try:
    tty.setcbreak(sys.stdin.fileno())

    i = 0
    while 1:
        print i
        i += 1

        if isData():
            c = sys.stdin.read(1)
            if c == ascii.ESC:
                break

finally:
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)

Edit: Changed the character detection to use characters as defined by curses.ascii, thanks to Daenyth's unhappiness with magic values which I share.

Muhammad Alkarouri
I feel like that `\x1b` should be a `KeyCodes.ESC` or a `key_codes['ESC']` defined elsewhere... Boo to magic values!
Daenyth
@Daenyth: I know the feeling. Things should be better now.
Muhammad Alkarouri
@Muhammed Alkarouri: And thanks to you for informing me of a module I didn't know about :)
Daenyth
A: 

This is how I implemented the functionality I wanted, but it's not EXACTLY what my question is about. So I'll post this in case anyone is looking for the same concept, but accept one of the more direct answers.


while True:
        try:
            time.sleep( 1 )
            #do stuff
        except KeyboardInterrupt:
            inp = raw_input( '>' )
            if inp == 'i':
                obj.integrate()
            elif inp == 'r':
                obj.reset()
Falmarri