亲宝软件园·资讯

展开

python连接池 python连接池实现示例程序

人气:0
想了解python连接池实现示例程序的相关内容吗,在本文为您仔细讲解python连接池的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,连接池,下面大家一起来学习吧。

复制代码 代码如下:

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!="":
                print "Message from ("+str(addr[0])+":"+str(addr[1])+"): "+sms
            else:
                print "Close the Connection from ("+str(addr[0])+":"+str(addr[1])+")"
                conn.close()
                break
        q.task_done()

if __name__=="__main__":
    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)
    print "Server is listening at 4242"

    for _ in range(0,thread_num):
        t=threading.Thread(target=worker)
        t.setDaemon(1)
        t.start()

    while 1:
        conn,addr=s.accept()
        print "Connection come from ("+str(addr[0])+":"+str(addr[1])+")"
        q.put((conn,addr))

    q.join()

加载全部内容

相关教程
猜你喜欢
用户评论