-
-
Save perrie625/30ee377d207c798ab4e9417dbfaf40c4 to your computer and use it in GitHub Desktop.
gevent nonblocking stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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