刚刚了解到这个类库,据说是个并发网络场景下非常高性能的模块
我自己写了个代码做了个测试,反而比直接使用多threading SocketServer模块更慢,是我用的有问题吗?求高手指教
我做了4组测试,分别是服务端使用/不使用eventlet 客户端使用/不使用eventlet的组合测试,结果是都不使用eventlet的一组情况最好,还请各位指教~
服务端代码
#import eventlet
#eventlet.monkey_patch()
#from eventlet.green import SocketServer
from SocketServer import TCPServer,StreamRequestHandler,ThreadingMixIn,UnixStreamServer,BaseServer
import os
class myserver(ThreadingMixIn, TCPServer):
#class myserver(ThreadingMixIn, UnixStreamServer):
request_queue_size = 8192
allow_reuse_address = True
def _remove_socket(self):
pass
try:
os.remove(self.server_address)
except OSError:
pass
def server_bind(self):
#self._remove_socket()
TCPServer.server_bind(self)
def shutdown(self):
TCPServer.shutdown(self)
self.server_close()
self._remove_socket()
class requesthandler(StreamRequestHandler):
def handle(self):
req = ''
while True:
chunk = self.rfile.readline()
if not chunk:
break
req += chunk
if chunk == '\r\n':
break
data = 'Hello World\r\n'
self.wfile.write('HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s'%(len(data),data))
def main():
#addr = './123.sock'
addr = ('127.0.0.1', 81)
try:
server = myserver(addr,requesthandler)
print server
server.serve_forever(0.1)
except KeyboardInterrupt:
server.shutdown()
main()
客户端代码
#import eventlet
#eventlet.monkey_patch()
import threading
import blist,socket
jobs = blist.blist([])
def a():
for i in xrange(100):
s = socket.socket()
s.connect(('127.0.0.1',81))
#s = socket.socket(socket.AF_UNIX)
#s.connect('./123.sock')
s.sendall('\r\n')
data = ''
ret = s.recv(1)
while True:
if ret:
data += ret
ret = s.recv(1)
else:
s.close()
break
cc = 300
for i in xrange(cc):
jobs.append(threading.Thread(target = a))
for i in xrange(cc):
jobs[i].start()
for i in xrange(cc):
jobs[i].join()