Navigation


sandbox: key.py

File key.py, 468 bytes (added by briegel@mpia.de, 10 years ago)
Line 
1
2import sys
3import select
4
5def something(line):
6  print('read input:', line)
7
8def 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.
13while 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)
20else:
21  something_else()
22