asyncore就是纯粹的异步socket,和c++使用异步socket没有什么大的区别,就是封装了loop等。实际上还是会有很多状态。
eventlet采用coroutine,本质上也是异步socket,但是已经是将异步socket同步化,没有任何状态(状态都保存在栈中),只有异步才需要考虑状态,同步根本没有状态之分。
所以eventlet明显比asycore要好用很多,也复杂很多。
贴一个asycore的例子:
#!/usr/bin/python import sys,os,time; import asyncore,socket; class HTTPClient(asyncore.dispatcher): def __init__(self, host, port, path): asyncore.dispatcher.__init__(self); self.create_socket(socket.AF_INET, socket.SOCK_STREAM); self.connect((host, int(port))); self.buffer = 'GET %s HTTP/1.1\r\nHost: %s\r\n\r\n'%(path, host); def handle_connect(self): pass; def handle_close(self): self.close(); def handle_read(self): print("[handle_read] event loop start to read."); print self.recv(8192); def writable(self): return len(self.buffer) > 0; def handle_write(self): sent = self.send(self.buffer); self.buffer = self.buffer[sent:]; class EchoServer(asyncore.dispatcher): def __init__(self, host, port): asyncore.dispatcher.__init__(self); self.create_socket(socket.AF_INET, socket.SOCK_STREAM); self.set_reuse_addr(); self.bind((host, int(port))); self.listen(10); def handle_accept(self): pair = self.accept(); if pair is None: return; (sock, addr) = pair; print("incoming client: %s"%(repr(addr))); handler = EchoHandler(sock); class EchoHandler(asyncore.dispatcher_with_send): def handle_read(self): data = self.recv(8192); if data is None: return; self.send("winlin"); time.sleep(3); self.send(data); check_mode = lambda : len(sys.argv) <= 1; check_mode_value = lambda: sys.argv[1] not in ("server", "client"); check_mode_argc = lambda mode,argc: sys.argv[1] == mode and len(sys.argv) != 2+argc; if check_mode() or check_mode_value() or check_mode_argc("client", 3) or check_mode_argc("server", 2): print("Usage: %s <mode>\n" "mode: the mode, server or client.\n" "if mode is client, must specifies: <host> <port> <path>\n" " host: the server ip or hostname. ie. dev\n" " port: the port to connect to. ie. 80\n" " path: the path to get. ie. /api/mine\n" "if mode is server, must specifies: <host> <port>\n" " host: the server ip or hostname. ie. dev\n" " port: the port to listen. ie. 80"%(sys.argv[0])); sys.exit(1); mode = sys.argv[1]; if mode == "client": (host, port, path) = sys.argv[2:]; client = HTTPClient(host, port, path); asyncore.loop(); else: (host, port) = sys.argv[2:]; server = EchoServer(host, port); asyncore.loop();
eventlet和st-threads一样的思路,都是提供替代函数,实现异步socket的同步化:
#!/usr/bin/python import sys,os,time,json; import eventlet; import eventlet.green.urllib2; # download and install: # http://eventlet.net/ # wget https://pypi.python.org/packages/source/e/eventlet/eventlet-0.13.0.tar.gz # tar xf eventlet-0.13.0.tar.gz && cd eventlet-0.13.0 && sudo python setup.py install if len(sys.argv) <= 1: print("Usage: %s <connections>\n" "connections: the concurrency connection to request."%(sys.argv[0])); sys.exit(1); connections = int(sys.argv[1]); print("create %s concurrency connection to request api"%(connections)); pool = eventlet.GreenPool(connections+1); apis = { "cdn": "http://www.baidu.com", "key": "http://www.sina.com.cn" }; def task_function(id): # request cdn info. url = apis["cdn"]; print("[task][#%s] request cdn info from %s"%(id, url)); cdn_str = eventlet.green.urllib2.urlopen(url).read(); cdn_json = json.loads(cdn_str); print("[task][#%s] timestamp=%s"%(id, cdn_json["timestamp"])); # request key info. url = apis["key"]; print("[task][#%s] request cdn info from %s"%(id, url)); key_str = eventlet.green.urllib2.urlopen(url).read(); key_json = json.loads(key_str); print("[task][#%s] len(key_pieces)=%s"%(id, len(key_json["key_pieces"]))); def deamon(): while True: print("green threads: running=%s waiting=%s"%(pool.running(), pool.waiting())); eventlet.green.time.sleep(0.5); pool.spawn_n(deamon); for i in range(0, connections): pool.spawn_n(task_function, i); pool.waitall(); print("all task finished.");