python non blocking input

Solutions on MaxInterview for python non blocking input by the best coders in the world

showing results for - "python non blocking input"
Frida
02 Feb 2020
1#! /usr/bin/python3
2
3"""Treat input if available, otherwise exit without
4blocking"""
5
6import sys
7import select
8
9def something(line):
10  print('read input:', line, end='')
11
12def something_else():
13  print('no input')
14
15# If there's input ready, do something, else do something
16# else. Note timeout is zero so select won't block at all.
17while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
18  line = sys.stdin.readline()
19  if line:
20    something(line)
21  else: # an empty line means stdin has been closed
22    print('eof')
23    exit(0)
24else:
25  something_else()
26