gevent.monkey – Make the standard library cooperative

patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, httplib=False, subprocess=True, sys=False, aggressive=True, Event=False, builtins=True)

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

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.


Replace SSLSocket object and socket wrapping functions in ssl with cooperative versions.

This is only useful if patch_socket() has been called.


Replace os.fork() with gevent.fork(), and, on POSIX, os.waitpid() with gevent.os.waitpid() (if the environment variable GEVENT_NOWAITPID is not defined). Does nothing if fork is not available.


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


Replace with

If aggressive is true (the default), also remove other blocking functions from select and (on Python 3.4 and above) selectors.

patch_thread(threading=True, _threading_local=True, Event=False, logging=True, existing_locks=True)

Replace the standard thread module to make it greenlet-based.

  • If threading is true (the default), also patch threading.
  • If _threading_local is true (the default), also patch _threading_local.local.
  • If logging is True (the default), also patch locks taken if the logging module has been configured.
  • If existing_locks is True (the default), and the process is still single threaded, make sure than any threading.RLock (and, under Python 3, importlib._bootstrap._ModuleLock) instances that are currently locked can be properly unlocked.

Replace, subprocess.check_call(), subprocess.check_output() and subprocess.Popen() with cooperative versions.

patch_sys(stdin=True, stdout=True, stderr=True)

Patch sys.std[in,out,err] to use a cooperative IO via a threadpool.

This is relatively dangerous and can have unintended consequences such as hanging the process or misinterpreting control keys when input and raw_input are used.

This method does nothing on Python 3. The Python 3 interpreter wants to flush the TextIOWrapper objects that make up stderr/stdout at shutdown time, but using a threadpool at that time leads to a hang.

get_original(mod_name, item_name)

Retrieve the original object from a module.

If the object has not been patched, then that object will still be retrieved.

Parameters:item_name – A string or sequenc of strings naming the attribute(s) on the module mod_name to return.
Returns:The original value if a string was given for item_name or a sequence of original values if a sequence was passed.

Check if a module has been replaced with a cooperative version.

is_object_patched(modname, objname)

Check if an object in a module has been replaced with a cooperative version.

Next page: gevent.pool – Managing greenlets in a group