鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 编程语言开发 > python > >

[原创]python连接池的实现

来源:互联网 作者:佚名 时间:2013-12-10 13:28
import socket import Queue import threading def worker(): while True:i = q.get()conn = i[0]addr =i[1 ] while 1 :sms =conn.recv(1024 ) if sms!= "" : +str(addr[0])++str(addr[1])++ sms else : +str(addr[0])++str(addr[1])+ conn.close() break q.

import socket import Queue import threading def worker(): while True: i = q.get() conn=i[0] addr=i[1] while 1: sms=conn.recv(1024) if sms!="": +str(addr[0])++str(addr[1])++sms else: +str(addr[0])++str(addr[1])+ conn.close() break q.task_done() : q = Queue.Queue() thread_num=5000 s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) s.bind(("",4242)) s.listen(50000) _ in range(0,thread_num): t=threading.Thread(target=worker) t.setDaemon(1) t.start() while 1: conn,addr=s.accept() +str(addr[0])++str(addr[1])+ q.put((conn,addr)) q.join()

网友评论
<