Skip to content

Instantly share code, notes, and snippets.

@damoxc
Created April 14, 2011 12:42
Show Gist options
  • Save damoxc/919394 to your computer and use it in GitHub Desktop.
Save damoxc/919394 to your computer and use it in GitHub Desktop.
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