日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中select怎么用

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

發(fā)展壯大離不開廣大客戶長期以來的信賴與支持,我們將始終秉承“誠信為本、服務(wù)至上”的服務(wù)理念,堅持“二合一”的優(yōu)良服務(wù)模式,真誠服務(wù)每家企業(yè),認(rèn)真做好每個細(xì)節(jié),不斷完善自我,成就企業(yè),實現(xiàn)共贏。行業(yè)涉及成都衛(wèi)生間隔斷等,在重慶網(wǎng)站建設(shè)全網(wǎng)整合營銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計、軟件開發(fā)等項目上具有豐富的設(shè)計經(jīng)驗。

注意:Python的select()方法適用于UNIX操作系統(tǒng),不支持Windows操作系統(tǒng)

接下來通過echo server例子要以了解select 是如何通過單進(jìn)程實現(xiàn)同時處理多個非阻塞的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個通信列表, 第一個是所有的輸入的data,就是指外部發(fā)過來的數(shù)據(jù),第2個是監(jiān)控和接收所有要發(fā)出去的data(outgoing data),第3個監(jiān)控錯誤信息,接下來我們需要創(chuàng)建2個列表來包含輸入和輸出信息來傳給select()。

# Sockets from which we expect to read
inputs = [ server ]
 
# Sockets to which we expect to write
outputs = [ ]

所有客戶端的進(jìn)來的連接和數(shù)據(jù)將會被server的主循環(huán)程序放在上面的list中處理,我們現(xiàn)在的server端需要等待連接可寫(writable)之后才能過來,然后接收數(shù)據(jù)并返回(因此不是在接收到數(shù)據(jù)之后就立刻返回),因為每個連接要把輸入或輸出的數(shù)據(jù)先緩存到queue里,然后再由select取出來再發(fā)。

# Outgoing message queues (socket:Queue)
message_queues = {}

通過服務(wù)器主循環(huán)將連接添加到這些列表并從這些列表中移除。由于服務(wù)器的這個版本在發(fā)送任何數(shù)據(jù)之前將等待套接字變得可寫(而不是立即發(fā)送應(yīng)答),所以每個輸出連接都需要一個隊列作為要通過它發(fā)送的數(shù)據(jù)的緩沖區(qū)。

下面是此程序的主循環(huán),調(diào)用select()時會阻塞和等待直到新的連接和數(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個新的list,我們上面將他們分別賦值為readable,

writable,exceptional, 所有在readable list中的socket連接代表有數(shù)據(jù)可接收(recv),所有在writable list中的存放著你可以對其進(jìn)行發(fā)送(send)操作的socket連接,當(dāng)連接通信出現(xiàn)error時會把error寫到exceptional列表中。

Readable list 中的socket 可以有3種可能狀態(tài),第一種是如果這個socket是main "server" socket,它負(fù)責(zé)監(jiān)聽客戶端的連接,如果這個main server socket出現(xiàn)在readable里,那代表這是server端已經(jīng)ready來接收一個新的連接進(jìn)來了,為了讓這個main server能同時處理多個連接,在下面的代碼里,我們把這個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()

第二種情況是這個socket是已經(jīng)建立了的連接,它把數(shù)據(jù)發(fā)了過來,這個時候你就可以通過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)

第三種情況就是這個客戶端已經(jīng)斷開了,所以你再通過recv()接收到的數(shù)據(jù)就為空了,所以這個時候你就可以把這個跟客戶端的連接關(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)  #既然客戶端都斷開了,我就不用再給它返回數(shù)據(jù)了,所以這時候如果這個客戶端的連接對象還在outputs列表中,就把它刪掉。

inputs.remove(s)    #inputs中也刪除掉
s.close()        #把這個連接關(guān)閉掉
  
# Remove message queue
del message_queues[s]

對于writable list中的socket,也有幾種狀態(tài),如果這個客戶端連接在跟它對應(yīng)的queue里有數(shù)據(jù),就把這個數(shù)據(jù)取出來再發(fā)回給這個客戶端,否則就把這個連接從output list中移除,這樣下一次循環(huán)select()調(diào)用時檢測到outputs list中沒有這個連接,那就會認(rèn)為這個連接還處于非活動狀態(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)

最后,如果在跟某個socket連接通信過程中出了錯誤,就把這個連接對象在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]

文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:python中select怎么用
地址分享:http://m.5511xx.com/article/cdiepse.html