Created
April 8, 2011 22:32
-
-
Save coopernurse/910860 to your computer and use it in GitHub Desktop.
gevent concurrency test script
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 | |
import sys | |
import gevent | |
from gevent import Greenlet | |
from gevent.pool import Pool | |
from gevent.server import StreamServer | |
from gevent.socket import create_connection | |
bind_addr = '127.0.0.1' | |
port = 32392 | |
clients = int(sys.argv[1]) | |
successful_clients = 0 | |
client_count = 0 | |
server_count = 0 | |
def server_conn(sock, addr): | |
global server_count | |
server_count += 1 | |
f = sock.makefile() | |
line = f.readline() | |
while line: | |
gevent.sleep(0.1) | |
f.write(line) | |
f.flush() | |
line = f.readline() | |
def start_server(): | |
pool = Pool(5000) | |
server = StreamServer((bind_addr, port), | |
server_conn, | |
spawn=pool) | |
server.serve_forever() | |
def start_client(): | |
global successful_clients, client_count | |
client_count += 1 | |
line = "hi there\n" | |
s = create_connection((bind_addr, port)) | |
f = s.makefile() | |
f.write(line) | |
f.flush() | |
gevent.sleep(0) | |
assert line == f.readline() | |
s.close() | |
successful_clients += 1 | |
Greenlet.spawn(lambda: start_server()) | |
gl = [] | |
for i in range(clients): | |
gl.append(Greenlet.spawn(lambda: start_client())) | |
for g in gl: | |
g.join() | |
print "total clients: %d" % clients | |
print " successful: %d" % successful_clients | |
print " client conn: %d" % client_count | |
print " server conn: %d" % server_count |
You should replace
gl.append(Greenlet.spawn(lambda: start_client()))
with
gl.append(gevent.spawn(start_client))
Also, instead of this:
def start_server():
pool = Pool(5000)
server = StreamServer((bind_addr, port),
server_conn,
spawn=pool)
server.serve_forever()
Greenlet.spawn(lambda: start_server())
you should just do this:
def start_server():
pool = Pool(5000)
server = StreamServer((bind_addr, port),
server_conn,
spawn=pool)
server.start()
start_server()
BTW, if you want to limit the number of simultaneous client connections, you should also use a Pool:
clients_pool = Pool(1000)
for i in xrange(clients):
clients_pool.spawn(start_client) # will block if the pool is full until there's a free slot
clients_pool.join()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BTW, the gevent.sleep() calls were added later. It fails similarly with or without them.