Python編程scoketServer實現(xiàn)多線程同步實例代碼
本文研究的主要是Python編程scoketServer實現(xiàn)多線程同步的相關(guān)內(nèi)容,具體介紹如下。
開發(fā)過程中,為了實現(xiàn)不同的客戶端同一時刻只能有一個使用共同數(shù)據(jù)。
雖說用Python編寫簡單的網(wǎng)絡(luò)程序很方便,但復雜一點的網(wǎng)絡(luò)程序還是用現(xiàn)成的框架比較好。這樣就可以專心事務(wù)邏輯,而不是套接字的各種細節(jié)。SocketServer模塊簡化了編寫網(wǎng)絡(luò)服務(wù)程序的任務(wù)。同時SocketServer模塊也是Python標準庫中很多服務(wù)器框架的基礎(chǔ)。
網(wǎng)絡(luò)服務(wù)類:
SocketServer提供了4個基本的服務(wù)類:
TCPServer針對TCP套接字流
UDPServer針對UDP數(shù)據(jù)報套接字
UnixStreamServer和UnixDatagramServer針對UNIX域套接字,不常用。
首先,明確一點,在scoketServer中,每當有一個客戶端連接成功后都會為每個客戶端創(chuàng)建一個線程。
為了讓這些多線程之間能夠同步執(zhí)行,我的做法是:再創(chuàng)建一個線程類,這個線程類中做一些我的項目需要做的事情,,當某個客戶端想成使用到這個線程時,給當前線程加鎖,運行完成后釋放鎖。
請指教
詳細步驟請看注釋:
#coding=gbk __author__ = 'kaikai' import Queue import threading import time import SocketServer #全局線程鎖 threadLock = threading.Lock()#全局數(shù)據(jù)隊列 data = Queue.Queue() #工作線程類, class testThead(threading.Thread): global data def __init__(self): threading.Thread.__init__(self) def begin_test(self): self.start() def run(self): global threadLock threadLock.acquire() # 從隊列中取出連接和數(shù)據(jù) if data.qsize()>0: this_receive = data.get() else: print "data size is empty !" return # 解析數(shù)據(jù),獲得連接和數(shù)據(jù) # 使用當前數(shù)據(jù)的conn this_conn = this_receive.keys()[0] this_data = this_receive[this_conn] # 釋放鎖 threadLock.release() def send_msg(self,conn,msg): try: conn.sendall(msg) except Exception as e: print "send " + str(msg) +"fail !!!!!!!!!!!!!!" def recv_msg(self,conn): try: recv_msg = conn.recv(2048) return recv_msg except Exception as e: print " recv msg fail !!!!!!!!!!" return None # 每有一個客戶端生成一個線程。所有線程調(diào)用同一個測試線程,如果測試線程在鎖定中,則進入等待。 class MyServer(SocketServer.BaseRequestHandler): def send_msg(self,conn,msg): try: conn.sendall(msg) except Exception as e: print "send " + str(msg) +"fail !!!!!!!!!!!!!!" def recv_msg(self,conn): try: recv_msg = conn.recv(2048) return recv_msg except Exception as e: print " recv msg fail !!!!!!!!!!" def handle(self): global data # 獲得連接 conn = self.request print "client connect!" # 循環(huán)接受客戶端數(shù)據(jù) while True: # 接受客戶端發(fā)送過來的參數(shù) receive_data = self.recv_msg(conn) print receive_data # 如果參數(shù)為空,返回報錯 結(jié)束循環(huán) if not receive_data: print "can not get data form client ! " break print "data size put before: " + str(data.qsize()) # 將連接和數(shù)據(jù)添加到隊列中 放入連接可以保證在另一個線程中直接使用連接給相應(yīng)客戶端發(fā)送或者接受數(shù)據(jù)。同時保證數(shù)據(jù)與客戶端的一一對應(yīng) data.put({conn:receive_data}) print "data size put aftter: " + str(data.qsize()) # 初始化測試線程 testThead_this = testThead() # 開始測試線程 testThead_this.begin_test() # testThead_this.start() # 等待測試線程執(zhí)行結(jié)束 testThead_this.join() print "this test end " if __name__ == "__main__" : try: server = SocketServer.ThreadingTCPServer(('192.168.100.100',56780),MyServer) server.timeout = 100 print "Server run success !!!! " server.serve_forever() except Exception as e: print "Server run failed !!!!\n error: " + str(e)
總結(jié)
以上就是本文關(guān)于Python編程scoketServer實現(xiàn)多線程同步實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關(guān)文章
Python 進程之間共享數(shù)據(jù)(全局變量)的方法
今天小編就為大家分享一篇Python 進程之間共享數(shù)據(jù)(全局變量)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07python爬蟲開發(fā)之使用python爬蟲庫requests,urllib與今日頭條搜索功能爬取搜索內(nèi)容實例
這篇文章主要介紹了python爬蟲開發(fā)之使用python爬蟲庫requests,urllib與今日頭條搜索功能爬取搜索內(nèi)容實例,需要的朋友可以參考下2020-03-03Python 2.7.x 和 3.x 版本的重要區(qū)別小結(jié)
這篇文章主要介紹了Python 2.7.x 和 3.x 版本的重要區(qū)別小結(jié),需要的朋友可以參考下2014-11-11python如何使用雙線性插值計算網(wǎng)格內(nèi)數(shù)據(jù)
這篇文章主要介紹了python如何使用雙線性插值計算網(wǎng)格內(nèi)數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08解決運行django程序出錯問題 ''str''object has no attribute''_meta''
這篇文章主要介紹了解決運行django程序出錯問題 'str'object has no attribute'_meta',具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07