Skip to content

Instantly share code, notes, and snippets.

@perrie625
Forked from tmc/gist:787105
Created July 3, 2017 08:20
Show Gist options
  • Save perrie625/30ee377d207c798ab4e9417dbfaf40c4 to your computer and use it in GitHub Desktop.
Save perrie625/30ee377d207c798ab4e9417dbfaf40c4 to your computer and use it in GitHub Desktop.
gevent nonblocking stdin
import os
import sys
import fcntl
import gevent
from gevent.socket import wait_read
def print_every(s, repeat=1):
print s
if repeat:
gevent.spawn_later(repeat, print_every, s, repeat)
def echo_stdin():
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NONBLOCK)
while True:
wait_read(sys.stdin.fileno())
l = sys.stdin.readline()
print l
if l == 'quit':
break
print_every('printing every second')
gevent.spawn(echo_stdin).join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment