sandbox: key.py
File key.py, 468 bytes (added by , 10 years ago) |
---|
Line | |
---|---|
1 | |
2 | import sys |
3 | import select |
4 | |
5 | def something(line): |
6 | print('read input:', line) |
7 | |
8 | def something_else(): |
9 | print('no input') |
10 | |
11 | # If there's input ready, do something, else do something |
12 | # else. Note timeout is zero so select won't block at all. |
13 | while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: |
14 | line = sys.stdin.readline() |
15 | if line: |
16 | something(line) |
17 | else: # an empty line means stdin has been closed |
18 | print('eof') |
19 | exit(0) |
20 | else: |
21 | something_else() |
22 |