Skip to content

Instantly share code, notes, and snippets.

@schlamar
Last active Aug 29, 2015
Embed
What would you like to do?
import functools
import os
import socket
import time
import pyuv
def on_connection(message, handle, loop):
conn = pyuv.TCP(handle.loop)
handle.accept(conn)
time.sleep(3)
conn.write('message from %s\n' % message)
conn.close()
if message == 'child':
handle.close()
def handle_fd(message, fd):
loop = pyuv.Loop.default_loop()
handle = pyuv.TCP(loop)
handle.open(os.dup(fd))
callback = functools.partial(on_connection, message)
handle.listen(callback)
loop.run()
def main():
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setblocking(False)
serversocket.bind(("127.0.0.1", 8888))
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.listen(0)
if os.fork() == 0:
while True:
handle_fd("child", serversocket.fileno())
else:
handle_fd("parent", serversocket.fileno())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment