如果我有与名为sock
的解锁套接字相关的代码
recv_data = sock.recv(1024)
if recv_data:
data.outb += recv_data
else:
print('closing connection to', data.addr)
sel.unregister(sock)
sock.close()
究竟会发生什么?语句recv_data = sock.recv(1024)
与阻塞和非阻塞有何不同?如果程序不等待该语句完成,那么recv_data
会有什么价值?
编辑: 完整的服务器代码:
import selectors
import socket
import types
host = "127.0.0.1"
port = 65432
def accept_wrapper(sock):
conn, addr = sock.accept()
print('accepted conneciton from', addr)
conn.setblocking(False)
data = types.SimpleNamespace(addr=addr, inb=b'', outb=b'')
events = selectors.EVENT_READ | selectors.EVENT_WRITE
sel.register(conn, events, data=data)
def service_connection(key, mask):
sock = key.fileobj
data = key.data
if mask & selectors.EVENT_READ:
recv_data = sock.recv(1024)
if recv_data:
data.outb += recv_data
else:
print('closing connection to', data.addr)
sel.unregister(sock)
sock.close()
if mask & selectors.EVENT_WRITE:
if data.outb:
print('echoing', repr(data.outb), 'to', data.addr)
sent = sock.send(data.outb)
data.outb = data.outb[sent:]
sel = selectors.DefaultSelector()
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsock.bind((host, port))
lsock.listen()
print('listening on', (host, port))
lsock.setblocking(False)
sel.register(lsock, selectors.EVENT_READ, data=None)
while True:
events = sel.select(timeout=None)
for key, mask in events:
if key.data is None:
# From the listening socket
accept_wrapper(key.fileobj)
else:
# Client Socket
service_connection(key, mask)
答案 0 :(得分:0)
如果sock
是非阻塞的,否则它将阻塞BlockingIOError
。代码周围是否没有显示try
/ except
?另一个选项是select.select
,用于确保在调用recv
之前读取了要接收的数据。
示例假设合适的服务器:
>>> from socket import *
>>> s=socket()
>>> s.connect(('localhost',5000))
>>> s.setblocking(0)
>>> s.recv(1024)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BlockingIOError: [WinError 10035] A non-blocking socket operation could not be completed immediately
编辑:
使用附加代码,仅在recv
报告sel.select
(准备读取的数据)时调用EVENT_READ
,因此recv
不应阻塞。它将返回数据或一个空字符串。后者表明套接字已关闭。