一执桌、select原理
網(wǎng)絡(luò)通信被Unix系統(tǒng)抽象為文件的讀寫,通常是一個(gè)設(shè)備芜赌,由設(shè)備驅(qū)動(dòng)程序提供仰挣,驅(qū)動(dòng)可以知道自身的數(shù)據(jù)是否可用。支持阻塞操作的設(shè)備驅(qū)動(dòng)通常會實(shí)現(xiàn)一組自身的等待隊(duì)列较鼓,如讀/寫等待隊(duì)列用于支持上層(用戶層)所需的block或non-block操作椎木。設(shè)備的文件的資源如果可用(可讀或者可寫)則會通知進(jìn)程,反之則會讓進(jìn)程睡眠博烂,等到數(shù)據(jù)到來可用的時(shí)候,再喚醒進(jìn)程漱竖。
這些設(shè)備的文件描述符被放在一個(gè)數(shù)組中禽篱,然后select調(diào)用的時(shí)候遍歷這個(gè)數(shù)組,如果對于的文件描述符可讀則會返回改文件描述符馍惹。當(dāng)遍歷結(jié)束之后躺率,如果仍然沒有一個(gè)可用設(shè)備文件描述符玛界,select讓用戶進(jìn)程則會睡眠,直到等待資源可用的時(shí)候在喚醒悼吱,遍歷之前那個(gè)監(jiān)視的數(shù)組慎框。每次遍歷都是線性的。
二后添、select方法
Python的select()方法直接調(diào)用操作系統(tǒng)的IO接口笨枯,它監(jiān)控sockets,open files, and pipes(所有帶fileno()方法的文件句柄)何時(shí)變成readable 和writeable, 或者通信錯(cuò)誤,select()使得同時(shí)監(jiān)控多個(gè)連接變的簡單遇西,并且這比寫一個(gè)長循環(huán)來等待和監(jiān)控多客戶端連接要高效馅精,因?yàn)閟elect直接通過操作系統(tǒng)提供的C的網(wǎng)絡(luò)接口進(jìn)行操作,而不是通過Python的解釋器粱檀。
示例(只支持Unix洲敢,不支持Windows):
服務(wù)端:
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)
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().
readable , writable , exceptional = select.select(inputs, outputs, inputs, timeout)
所有客戶端的進(jìn)來的連接和數(shù)據(jù)將會被server的主循環(huán)程序放在上面的list中處理,我們現(xiàn)在的server端需要等待連接可寫(writable)之后才能過來渗常,然后接收數(shù)據(jù)并返回(因此不是在接收到數(shù)據(jù)之后就立刻返回)哮塞,因?yàn)槊總€(gè)連接要把輸入或輸出的數(shù)據(jù)先緩存到queue里,然后再由select取出來再發(fā)出去
當(dāng)你把inputs,outputs,exceptional(這里跟inputs共用)傳給select()后凳谦,它返回3個(gè)新的list忆畅,我們上面將他們分別賦值為readable,writable,exceptional, 所有在readable list中的socket連接代表有數(shù)據(jù)可接收(recv),所有在writable list中的存放著你可以對其進(jìn)行發(fā)送(send)操作的socket連接,當(dāng)連接通信出現(xiàn)error時(shí)會把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è)客戶端的連接對象還在outputs列表中,就把它刪掉
inputs.remove(s) #inputs中也刪除掉
s.close() #把這個(gè)連接關(guān)閉掉
# Remove message queue
del message_queues[s]
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è)客戶端的連接對象還在outputs列表中,就把它刪掉
inputs.remove(s) #inputs中也刪除掉
s.close() #把這個(gè)連接關(guān)閉掉
# Remove message queue
del message_queues[s]
對于writable list中的socket笋庄,也有幾種狀態(tài)效扫,如果這個(gè)客戶端連接在跟它對應(yīng)的queue里有數(shù)據(jù),就把這個(gè)數(shù)據(jù)取出來再發(fā)回給這個(gè)客戶端无切,否則就把這個(gè)連接從output list中移除荡短,這樣下一次循環(huán)select()調(diào)用時(shí)檢測到outputs list中沒有這個(gè)連接,那就會認(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è)連接對象在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]
客戶端
下面的這個(gè)是客戶端程序展示了如何通過select()對socket進(jìn)行管理并與多個(gè)連接同時(shí)進(jìn)行交互
import socket
import sys
messages = [ 'This is the message. ',
'It will be sent ',
'in parts.',
]
server_address = ('localhost', 10000)
# Create a TCP/IP socket
socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
]
# Connect the socket to the port where the server is listening
print >>sys.stderr, 'connecting to %s port %s' % server_address
for s in socks:
s.connect(server_address)
接下來通過循環(huán)通過每個(gè)socket連接給server發(fā)送和接收數(shù)據(jù)
for message in messages:
# Send messages on both sockets
for s in socks:
print >>sys.stderr, '%s: sending "%s"' % (s.getsockname(), message)
s.send(message)
# Read responses on both sockets
for s in socks:
data = s.recv(1024)
print >>sys.stderr, '%s: received "%s"' % (s.getsockname(), data)
if not data:
print >>sys.stderr, 'closing socket', s.getsockname()
服務(wù)端完整代碼
#_*_coding:utf-8_*_
import select
import socket
import sys
import queue
# Create a TCP/IP socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(False)
# 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)
# Sockets from which we expect to read
inputs = [ server ]
# Sockets to which we expect to write
outputs = [ ]
message_queues = {}
while inputs:
# Wait for at least one of the sockets to be ready for processing
print( '\nwaiting for the next event')
readable, writable, exceptional = select.select(inputs, outputs, inputs)
# 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('new connection from', client_address)
connection.setblocking(False)
inputs.append(connection)
# Give the connection a queue for data we want to send
message_queues[connection] = queue.Queue()
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)
else:
# Interpret empty result as closed connection
print('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è)客戶端的連接對象還在outputs列表中,就把它刪掉
inputs.remove(s) #inputs中也刪除掉
s.close() #把這個(gè)連接關(guān)閉掉
# Remove message queue
del message_queues[s]
# 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('output queue for', s.getpeername(), 'is empty')
outputs.remove(s)
else:
print( 'sending "%s" to %s' % (next_msg, s.getpeername()))
s.send(next_msg)
# Handle "exceptional conditions"
for s in exceptional:
print('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]
客戶端完整代碼
import socket
import sys
messages = [ 'This is the message. ',
'It will be sent ',
'in parts.',
]
server_address = ('localhost', 10000)
# Create a TCP/IP socket
socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
socket.socket(socket.AF_INET, socket.SOCK_STREAM),
]
# Connect the socket to the port where the server is listening
print >>sys.stderr, 'connecting to %s port %s' % server_address
for s in socks:
s.connect(server_address)
for message in messages:
# Send messages on both sockets
for s in socks:
print >>sys.stderr, '%s: sending "%s"' % (s.getsockname(), message)
s.send(message)
# Read responses on both sockets
for s in socks:
data = s.recv(1024)
print >>sys.stderr, '%s: received "%s"' % (s.getsockname(), data)
if not data:
print >>sys.stderr, 'closing socket', s.getsockname()
s.close()
[本文轉(zhuǎn)自:http://www.cnblogs.com/alex3714/p/4372426.html#top]