阻塞 socket:
直到发送完所有数据,写操作才返回
非阻塞 socket:
阻塞 socket:
非阻塞 socket:
阻塞 socket:
非阻塞 socket:
阻塞 socket:
非阻塞 socket:
Python 示例(on Linux):
xxxxxxxxxx
import errno
import socket
import select
if "EPOLLRDHUP" not in dir(select):
select.EPOLLRDHUP = 0x2000
epoll = select.epoll()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setblocking(False)
try:
sock.connect(("127.0.0.1", 80))
except IOError as ex:
if ex.errno != errno.EINPROGRESS:
raise
epoll.register(sock.fileno(),
select.EPOLLIN | select.EPOLLOUT |
select.EPOLLERR | select.EPOLLHUP |
select.EPOLLRDHUP)
while True:
events = epoll.poll(timeout=0.02)
if not events:
continue
for fd, event in events:
if event & select.EPOLLIN:
print("in")
if event & select.EPOLLOUT:
print("out")
if event & select.EPOLLERR:
print("err")
if event & select.EPOLLHUP:
print("hup")
if event & select.EPOLLRDHUP:
print("rdhup")
break
sock.close()
Posix 定义了两条与 select 和非阻塞 connect 相关的规定: