Created
April 14, 2011 12:42
-
-
Save damoxc/919394 to your computer and use it in GitHub Desktop.
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
import os | |
import sys | |
import gevent | |
from gevent import socket | |
from gevent.server import StreamServer | |
acceptor = socket.socket() | |
acceptor.bind(('localhost', 4242)) | |
acceptor.listen(10) | |
def handle(conn, addr): | |
flo = conn.makefile() | |
flo.write('Child %s echo> ' % childpid) | |
flo.flush() | |
message = flo.readline() | |
flo.write(message) | |
conn.close() | |
print "Child %s echo'd: %r" % \ | |
(childpid, message.strip()) | |
for i in xrange(3): | |
pid = os.fork() | |
if pid == 0: | |
childpid = os.getpid() | |
print "Child %s listening on localhost:4242" % childpid | |
# Call event_reinit() | |
gevent.reinit() | |
try: | |
server = StreamServer(acceptor, handle) | |
server.serve_forever() | |
except KeyboardInterrupt: | |
sys.exit() | |
try: | |
os.waitpid(-1, 0) | |
except KeyboardInterrupt: | |
print "\nbailing" | |
acceptor.close() | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment