怎么可能gevent socket只阻止当前的greenlet?

时间:2013-06-24 02:34:49

标签: python sockets gevent

我正在阅读gevent.socket,但我不明白。

def recv(self, *args):
    sock = self._sock  # keeping the reference so that fd is not closed during waiting
    while True:
        try:
            return sock.recv(*args)
        except error, ex:
            if ex[0] == EBADF:
                return ''
            if ex[0] != EWOULDBLOCK or self.timeout == 0.0:
                raise
            # QQQ without clearing exc_info test__refcount.test_clean_exit fails
            sys.exc_clear()
        try:
            wait_read(sock.fileno(), timeout=self.timeout, event=self._read_event)
        except error, ex:
            if ex[0] == EBADF:
                return ''
            raise

sock中的recv_realsocket(family, type, proto)的实例。在socket.py中我找到了:

import _socket
_realsocket = _socket.socket

什么是_socket?为什么不会return sock.recv(*args)阻止整个程序?

1 个答案:

答案 0 :(得分:1)

_socket是python的标准c库,提供真正的网络通信, 和socket.py(在标准库或gevent中)将方法包装起来以供常用。

然后,在gevent.socket中查看类Socket的 init

```self._sock.setblocking(0)```

此语句使套接字对象无阻塞