
import sys
sys.path.insert(0, '../')
import signal
import threading
import pyuv


RESPONSE = "HTTP/1.1 200 OK\r\n" \
           "Content-Type: text/plain\r\n" \
           "Content-Length: 12\r\n" \
           "\r\n" \
           "hello world\n"


def on_client_shutdown(client):
    client.close()
    clients.remove(client)

def on_read(client, data):
    if data is None:
        client.close()
        clients.remove(client)
        return
    data = data.strip()
    if not data:
        return
    client.write(RESPONSE)
    client.shutdown(on_client_shutdown)

def on_connection(server):
    client = server.accept()
    clients.append(client)
    client.start_read(on_read)

def async_exit(async, data):
    [c.close() for c in clients]
    async.close()
    signal_h.close()
    server.close()

def signal_cb(sig, frame):
    async.send(async_exit)


print "PyUV version %s" % pyuv.__version__

loop = pyuv.Loop()

async = pyuv.Async(loop)
clients = []

server = pyuv.TCP(loop)
server.bind(("0.0.0.0", 1234))
server.listen(on_connection)

signal_h = pyuv.Signal(loop)
signal_h.start()

t = threading.Thread(target=loop.run)
t.start()

signal.signal(signal.SIGINT, signal_cb)
signal.pause()

t.join()

print "Stopped!"




import sys
sys.path.insert(0, '../')
import signal
import threading
import pyuv


RESPONSE = "HTTP/1.1 200 OK\r\n" \
           "Content-Type: text/plain\r\n" \
           "Content-Length: 12\r\n" \
           "\r\n" \
           "hello world\n"


def on_client_shutdown(client):
    client.close()
    clients.remove(client)

def on_read(client, data):
    if data is None:
        client.close()
        clients.remove(client)
        return
    data = data.strip()
    if not data:
        return
    client.write(RESPONSE)
    client.shutdown(on_client_shutdown)

def on_connection(server):
    client = server.accept()
    clients.append(client)
    client.start_read(on_read)

def async_exit(async, data):
    [c.close() for c in clients]
    async.close()
    signal_h.close()
    server.close()

def signal_cb(sig, frame):
    async.send(async_exit)


print "PyUV version %s" % pyuv.__version__

loop = pyuv.Loop.default_loop()

async = pyuv.Async(loop)
clients = []

server = pyuv.TCP(loop)
server.bind(("0.0.0.0", 1234))
server.listen(on_connection)

signal_h = pyuv.Signal(loop)
signal_h.start()

t = threading.Thread(target=loop.run)
t.start()

signal.signal(signal.SIGINT, signal_cb)
signal.pause()

t.join()

print "Stopped!"



Use default loop on TCP benchmark