Created
September 6, 2014 17:58
-
-
Save theacodes/e715ea093d961855d181 to your computer and use it in GitHub Desktop.
Non-blocking gevent stdin read
This file contains 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
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
there maybe a bug, when I use echo 123 | python echo.py,It run never end