新聞中心
python的select()方法直接調(diào)用操作系統(tǒng)的IO接口,它監(jiān)控sockets,open files, and pipes(所有帶fileno()方法的文件句柄)何時(shí)變成readable 和writeable, 或者通信錯(cuò)誤,select()使得同時(shí)監(jiān)控多個(gè)連接變的簡(jiǎn)單,并且這比寫一個(gè)長(zhǎng)循環(huán)來等待和監(jiān)控多客戶端連接要高效,因?yàn)閟elect直接通過操作系統(tǒng)提供的C的網(wǎng)絡(luò)接口進(jìn)行操作,而不是通過Python的解釋器。

發(fā)展壯大離不開廣大客戶長(zhǎng)期以來的信賴與支持,我們將始終秉承“誠(chéng)信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠(chéng)服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及成都衛(wèi)生間隔斷等,在重慶網(wǎng)站建設(shè)、全網(wǎng)整合營(yíng)銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。
注意:Python的select()方法適用于UNIX操作系統(tǒng),不支持Windows操作系統(tǒng)
接下來通過echo server例子要以了解select 是如何通過單進(jìn)程實(shí)現(xiàn)同時(shí)處理多個(gè)非阻塞的socket連接的。
import select
import socket
import sys
import Queue
# Create a TCP/IP socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
# Bind the socket to the port
server_address = ('localhost', 10000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
server.bind(server_address)
# Listen for incoming connections
server.listen(5)相關(guān)推薦:《Python基礎(chǔ)教程》
select()方法接收并監(jiān)控3個(gè)通信列表, 第一個(gè)是所有的輸入的data,就是指外部發(fā)過來的數(shù)據(jù),第2個(gè)是監(jiān)控和接收所有要發(fā)出去的data(outgoing data),第3個(gè)監(jiān)控錯(cuò)誤信息,接下來我們需要?jiǎng)?chuàng)建2個(gè)列表來包含輸入和輸出信息來傳給select()。
# Sockets from which we expect to read inputs = [ server ] # Sockets to which we expect to write outputs = [ ]
所有客戶端的進(jìn)來的連接和數(shù)據(jù)將會(huì)被server的主循環(huán)程序放在上面的list中處理,我們現(xiàn)在的server端需要等待連接可寫(writable)之后才能過來,然后接收數(shù)據(jù)并返回(因此不是在接收到數(shù)據(jù)之后就立刻返回),因?yàn)槊總€(gè)連接要把輸入或輸出的數(shù)據(jù)先緩存到queue里,然后再由select取出來再發(fā)。
# Outgoing message queues (socket:Queue)
message_queues = {}通過服務(wù)器主循環(huán)將連接添加到這些列表并從這些列表中移除。由于服務(wù)器的這個(gè)版本在發(fā)送任何數(shù)據(jù)之前將等待套接字變得可寫(而不是立即發(fā)送應(yīng)答),所以每個(gè)輸出連接都需要一個(gè)隊(duì)列作為要通過它發(fā)送的數(shù)據(jù)的緩沖區(qū)。
下面是此程序的主循環(huán),調(diào)用select()時(shí)會(huì)阻塞和等待直到新的連接和數(shù)據(jù)進(jìn)來。
# Wait for at least one of the sockets to be ready for processing print >>sys.stderr, '\nwaiting for the next event' readable, writable, exceptional = select.select(inputs, outputs, inputs)
當(dāng)你把inputs,outputs,exceptional(這里跟inputs共用)傳給select()后,它返回3個(gè)新的list,我們上面將他們分別賦值為readable,
writable,exceptional, 所有在readable list中的socket連接代表有數(shù)據(jù)可接收(recv),所有在writable list中的存放著你可以對(duì)其進(jìn)行發(fā)送(send)操作的socket連接,當(dāng)連接通信出現(xiàn)error時(shí)會(huì)把error寫到exceptional列表中。
Readable list 中的socket 可以有3種可能狀態(tài),第一種是如果這個(gè)socket是main "server" socket,它負(fù)責(zé)監(jiān)聽客戶端的連接,如果這個(gè)main server socket出現(xiàn)在readable里,那代表這是server端已經(jīng)ready來接收一個(gè)新的連接進(jìn)來了,為了讓這個(gè)main server能同時(shí)處理多個(gè)連接,在下面的代碼里,我們把這個(gè)main server的socket設(shè)置為非阻塞模式。
# Handle inputs for s in readable: if s is server: # A "readable" server socket is ready to accept a connection connection, client_address = s.accept() print >>sys.stderr, 'new connection from', client_address connection.setblocking(0) inputs.append(connection) # Give the connection a queue for data we want to send message_queues[connection] = Queue.Queue()
第二種情況是這個(gè)socket是已經(jīng)建立了的連接,它把數(shù)據(jù)發(fā)了過來,這個(gè)時(shí)候你就可以通過recv()來接收它發(fā)過來的數(shù)據(jù),然后把接收到的數(shù)據(jù)放到queue里,這樣你就可以把接收到的數(shù)據(jù)再傳回給客戶端了。
else: data = s.recv(1024) if data: # A readable client socket has data print >>sys.stderr, 'received "%s" from %s' % (data, s.getpeername()) message_queues[s].put(data) # Add output channel for response if s not in outputs: outputs.append(s)
第三種情況就是這個(gè)客戶端已經(jīng)斷開了,所以你再通過recv()接收到的數(shù)據(jù)就為空了,所以這個(gè)時(shí)候你就可以把這個(gè)跟客戶端的連接關(guān)閉了。
else: # Interpret empty result as closed connection print >>sys.stderr, 'closing', client_address, 'after reading no data' # Stop listening for input on the connection if s in outputs:
outputs.remove(s) #既然客戶端都斷開了,我就不用再給它返回?cái)?shù)據(jù)了,所以這時(shí)候如果這個(gè)客戶端的連接對(duì)象還在outputs列表中,就把它刪掉。
inputs.remove(s) #inputs中也刪除掉 s.close() #把這個(gè)連接關(guān)閉掉 # Remove message queue del message_queues[s]
對(duì)于writable list中的socket,也有幾種狀態(tài),如果這個(gè)客戶端連接在跟它對(duì)應(yīng)的queue里有數(shù)據(jù),就把這個(gè)數(shù)據(jù)取出來再發(fā)回給這個(gè)客戶端,否則就把這個(gè)連接從output list中移除,這樣下一次循環(huán)select()調(diào)用時(shí)檢測(cè)到outputs list中沒有這個(gè)連接,那就會(huì)認(rèn)為這個(gè)連接還處于非活動(dòng)狀態(tài)。
# Handle outputs for s in writable: try: next_msg = message_queues[s].get_nowait() except Queue.Empty: # No messages waiting so stop checking for writability. print >>sys.stderr, 'output queue for', s.getpeername(), 'is empty' outputs.remove(s) else: print >>sys.stderr, 'sending "%s" to %s' % (next_msg, s.getpeername()) s.send(next_msg)
最后,如果在跟某個(gè)socket連接通信過程中出了錯(cuò)誤,就把這個(gè)連接對(duì)象在inputs\outputs\message_queue中都刪除,再把連接關(guān)閉掉。
# Handle "exceptional conditions" for s in exceptional: print >>sys.stderr, 'handling exceptional condition for', s.getpeername() # Stop listening for input on the connection inputs.remove(s) if s in outputs: outputs.remove(s) s.close() # Remove message queue del message_queues[s]
網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:python中select怎么用
鏈接分享:http://fisionsoft.com.cn/article/cdiepse.html


咨詢
建站咨詢
