Created
April 5, 2011 20:53
-
-
Save coopernurse/904526 to your computer and use it in GitHub Desktop.
gevent - limit example
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 | |
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 | |
def server_conn(sock, addr): | |
pass | |
def start_server(): | |
pool = Pool(5000) | |
server = StreamServer((bind_addr, port), | |
server_conn, | |
spawn=pool) | |
server.serve_forever() | |
def start_client(): | |
global successful_clients | |
s = create_connection((bind_addr, port)) | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment