Python lso select.poll正在触发套接字读取POLLIN,当我们使用sokcet.recv读取时,我们得到的数据长度为0。闭合套接字上的yes select.poll将其标记为可读,但正如您所说的,即使这在所有网络中都不起作用,至少写操作应该失败。将在
Python lso select.poll正在触发套接字读取POLLIN,当我们使用sokcet.recv读取时,我们得到的数据长度为0。闭合套接字上的yes select.poll将其标记为可读,但正如您所说的,即使这在所有网络中都不起作用,至少写操作应该失败。将在,python,sockets,Python,Sockets,lso select.poll正在触发套接字读取POLLIN,当我们使用sokcet.recv读取时,我们得到的数据长度为0。闭合套接字上的yes select.poll将其标记为可读,但正如您所说的,即使这在所有网络中都不起作用,至少写操作应该失败。将在发生此问题的网络中使用现场测试结果进行更新上述测试不起作用;请参阅下面的详细信息。最终在应用程序层处理了保持活动的协议。 def _wait_for_socket_poller(self, read, write, message=No
lso select.poll正在触发套接字读取POLLIN,当我们使用sokcet.recv读取时,我们得到的数据长度为0。闭合套接字上的yes select.poll将其标记为可读,但正如您所说的,即使这在所有网络中都不起作用,至少写操作应该失败。将在发生此问题的网络中使用现场测试结果进行更新上述测试不起作用;请参阅下面的详细信息。最终在应用程序层处理了保持活动的协议。
def _wait_for_socket_poller(self, read, write, message=None):
"""
Instead of blockign wait, this polls and check if the read or write socket is ready. If so it proceeds with
reading or writing to the socket. The advantage is that while the poll blocks, it yeilds back to the other
waiting greenlets; poll blocks because we have not given a timeout
:param read: The read function
:param write: The write function
:param message: The CrowdBox API call
:return: The result : In case of read - In JSON format; But catch is that the caller cannot wait on the
result being available,as else the thread will block
"""
if not self.client_socket:
logging.error("CB ID =%d - Connection closed", self.id)
return
poller = select.poll()
# Commonly used flag setes
READ_ONLY = select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR
WRITE_ONLY = select.POLLOUT
READ_WRITE = READ_ONLY | select.POLLOUT
if read and write:
poller.register(self.client_socket, READ_WRITE)
elif write:
poller.register(self.client_socket, WRITE_ONLY)
elif read:
poller.register(self.client_socket, READ_ONLY)
# Map file descriptors to socket objects
fd_to_socket = {self.client_socket.fileno(): self.client_socket, }
result = ''
retry = True
while retry:
# Poll will Block!!
events = poller.poll(
1) # using poll instead of select as the latter runs out of file descriptors on load
# Note here, Poll needs to timeout or will block ,as there is no gevent patched poll, the moment it blocks
# neither greenlets or Twisted Deffered can help -Everything freezes,as all of this is in main thread
if not events:
retry = True
gevent.sleep(0) # This is needed to yeild in case no input comes from CB
else:
retry = False
clientsock = None
fd = None
flag = None
for fd, flag in events:
# Retrieve the actual socket from its file descriptor to map return of poll to socket
clientsock = fd_to_socket[fd]
if clientsock is None:
logging.error("Problem Houston")
raise ValueError("Client Sokcet has Become Invalid")
if flag & select.POLLHUP:
logging.error("Client Socket Closed")
self.client_socket.close()
self.client_socket = None
return None
if flag & (select.POLLIN | select.POLLPRI):
if read:
result = read()
if flag & select.POLLOUT:
if write:
result = write(message)
# poller.uregister(self.client_socket)
return result
sent = 0
try:
sent = self.client_socket.send(out)
except socket.error as e:
if e.args[0] == errno.EPIPE:
logging.error("Socket connection is closed or broken")
if sent == 0 and self.client_socket is not None:
logging.error("socket connection is already closed by client, cannot write request")
self.close_socket_connection()
else
# send succcessfully