gevent.monkey – Make the standard library cooperative

The functions in this module patch parts of the standard library with compatible cooperative counterparts from gevent package.

To patch an individual module call the corresponding patch_* function. For example, to patch socket module only, call patch_socket(). To patch all default modules, call gevent.monkey.patch_all().

Monkey can also patch thread and threading to become greenlet-based. So thread.start_new_thread() starts a new greenlet instead and threading.local becomes a greenlet-local storage.

Monkey patches:

  • socket module – patch_socket()
    • socket
    • SocketType
    • socketpair()
    • fromfd()
    • ssl() and sslerror
    • socket.getaddrinfo()
    • socket.gethostbyname()
    • It is possible to disable dns patching by passing dns=False to patch_socket() of patch_all()
    • If ssl is not available (Python < 2.6 without ssl and PyOpenSSL packages installed) then ssl() is removed from the target socket module.
  • ssl module – patch_ssl()
    • SSLSocket
    • wrap_socket()
    • get_server_certificate()
    • sslwrap_simple()
  • os module – patch_os()
    • fork()
  • time module – patch_time()
    • time()
  • select module – patch_select()
    • select()
    • Removes polling mechanisms that gevent.select does not simulate: poll, epoll, kqueue, kevent
  • thread and threading modules – patch_thread()
    • Become greenlet-based.
    • get_ident()
    • start_new_thread()
    • LockType
    • allocate_lock()
    • exit()
    • stack_size()
    • thread-local storage becomes greenlet-local storage
gevent.monkey.patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, httplib=False, aggressive=True)

Do all of the default monkey patching (calls every other function in this module.

gevent.monkey.patch_socket(dns=True, aggressive=True)

Replace the standard socket object with gevent’s cooperative sockets.

If dns is true, also patch dns functions in socket.

gevent.monkey.patch_ssl()
gevent.monkey.patch_os()

Replace os.fork() with gevent.fork().

gevent.monkey.patch_time()

Replace time.sleep() with gevent.sleep().

gevent.monkey.patch_select(aggressive=False)

Replace select.select() with gevent.select.select().

If aggressive is true (the default), also remove other blocking functions the select.

gevent.monkey.patch_thread(threading=True, _threading_local=True)

Replace the standard thread module to make it greenlet-based. If threading is true (the default), also patch threading.local. If _threading_local is true (the default), also patch _threading_local.local.

Previous topic

gevent.local – Greenlet-local objects

Next topic

gevent.core - event loop based on libev