gevent.socket – Cooperative low-level networking interface

This module provides socket operations and some related functions. The API of the functions and classes matches the API of the corresponding items in the standard socket module exactly, but the synchronous functions in this module only block the current greenlet and let the others run.

For convenience, exceptions (like error and timeout) as well as the constants from the socket module are imported into this module.

The exact API exposed by this module varies depending on what version of Python you are using. The documents below describe the API for Python 2 and Python 3, respectively.

Warning

All the described APIs should be imported from gevent.socket, and not from their implementation modules. Their organization is an implementation detail that may change at any time.

Gevent Extensions

Beyond the basic standard library interface, gevent.socket provides some extensions. These are identical and shared by all versions of Python.

Waiting

These functions are used to block the current greenlet until an open file (socket) is ready to perform I/O operations. These are low-level functions not commonly used by many programs.

Note

These use the underlying libev io watchers, which means that they share the same implementation limits. For example, on some platforms they can be used with more than just sockets, while on others the applicability is more limited.

wait_read(fileno, timeout=None, timeout_exc=<default value>)

Block the current greenlet until fileno is ready to read.

For the meaning of the other parameters and possible exceptions, see wait().

wait_write(fileno, timeout=None, timeout_exc=<default value>, event=<default value>)

Block the current greenlet until fileno is ready to write.

For the meaning of the other parameters and possible exceptions, see wait().

Parameters:event – Ignored. Applications should not pass this parameter. In the future, it may become an error.
wait_readwrite(fileno, timeout=None, timeout_exc=<default value>, event=<default value>)

Block the current greenlet until fileno is ready to read or write.

For the meaning of the other parameters and possible exceptions, see wait().

Parameters:event – Ignored. Applications should not pass this parameter. In the future, it may become an error.
wait(io, timeout=None, timeout_exc=<default value>)

Block the current greenlet until io is ready.

If timeout is non-negative, then timeout_exc is raised after timeout second has passed. By default timeout_exc is socket.timeout('timed out').

If cancel_wait() is called on io by another greenlet, raise an exception in this blocking greenlet (socket.error(EBADF, 'File descriptor was closed in another greenlet') by default).

Parameters:
  • io – A libev watcher, most commonly an IO watcher obtained from gevent.core.loop.io()
  • timeout_exc – The exception to raise if the timeout expires. By default, a socket.timeout exception is raised. If you pass a value for this keyword, it is interpreted as for gevent.timeout.Timeout.
cancel_wait(watcher, error=OSError(9, 'File descriptor was closed in another greenlet'))

See gevent.hub.Hub.cancel_wait()

Next page: gevent.ssl – Secure Sockets Layer (SSL/TLS) module