Skip to content

Instantly share code, notes, and snippets.

@tmc
Created January 19, 2011 23:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tmc/787105 to your computer and use it in GitHub Desktop.
Save tmc/787105 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()
@gabriel-samfira
Copy link

Sweet!

@btgoodwin
Copy link

This helped me; thank you!

@val314159
Copy link

Just found this, thx!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment