python連接池實現(xiàn)示例程序
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()
相關(guān)文章
python 數(shù)據(jù)庫查詢返回list或tuple實例
這篇文章主要介紹了python 數(shù)據(jù)庫查詢返回list或tuple實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python學習之a(chǎn)syncore模塊用法實例教程
這篇文章主要介紹了Python學習之a(chǎn)syncore模塊用法,主要講述了asyncore模塊的組成、原理及相關(guān)函數(shù)的用法,對于使用Python進行網(wǎng)絡(luò)編程來說非常實用,需要的朋友可以參考下2014-09-09pyqt4教程之實現(xiàn)windows窗口小示例分享
這篇文章主要介紹了pyqt4實現(xiàn)windows窗口小示例,需要的朋友可以參考下2014-03-03TensorFlow2.1.0安裝過程中setuptools、wrapt等相關(guān)錯誤指南
這篇文章主要介紹了TensorFlow2.1.0安裝時setuptools、wrapt等相關(guān)錯誤指南,本文通過安裝錯誤分析給出大家解決方案,感興趣的朋友跟隨小編一起看看吧2020-04-04python模塊hashlib(加密服務(wù))知識點講解
在本篇文章里小編給大家分享的是關(guān)于python模塊hashlib(加密服務(wù))知識點內(nèi)容,有需要的朋友們可以學習下。2019-11-11