Skip to content

Instantly share code, notes, and snippets.

@theacodes
Created September 6, 2014 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theacodes/e715ea093d961855d181 to your computer and use it in GitHub Desktop.
Save theacodes/e715ea093d961855d181 to your computer and use it in GitHub Desktop.
Non-blocking gevent stdin read
#!/usr/bin/env python
from gevent import monkey
monkey.patch_all()
monkey.patch_sys(stdin=True, stdout=False, stderr=False)
import gevent
from gevent.queue import Queue
import sys
import signal
q = Queue()
def producer():
while True:
line = sys.stdin.readline()
q.put(line)
def consumer():
while True:
val = q.get(True)
print "From consumer: %s" % val
def main():
gevent.signal(signal.SIGQUIT, gevent.kill)
greenlets = [
gevent.spawn(producer),
gevent.spawn(consumer)
]
try:
gevent.joinall(greenlets)
except KeyboardInterrupt:
print "Exiting..."
if __name__ == '__main__':
main()
@beruhan
Copy link

beruhan commented Dec 11, 2017

there maybe a bug, when I use echo 123 | python echo.py,It run never end

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