Commit 7033683b authored by Denis Bilenko's avatar Denis Bilenko

update monkey

- add patch_sys() which patches stdin,stdout,stderr
- add sys=False argument to patch_all()
- remove the module docstring (it's not up to date)
parent 4a5e3f6f
# Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details. # Copyright (c) 2009-2012 Denis Bilenko. See LICENSE for details.
"""Make the standard library cooperative. """Make the standard library cooperative."""
The functions in this module patch parts of the standard library with compatible cooperative counterparts
from :mod:`gevent` package.
To patch an individual module call the corresponding ``patch_*`` function. For example, to patch
socket module only, call :func:`patch_socket`. To patch all default modules, call ``gevent.monkey.patch_all()``.
Monkey can also patch thread and threading to become greenlet-based. So :func:`thread.start_new_thread`
starts a new greenlet instead and :class:`threading.local` becomes a greenlet-local storage.
Monkey patches:
* :mod:`socket` module -- :func:`patch_socket`
- :class:`socket`
- :class:`SocketType`
- :func:`socketpair`
- :func:`fromfd`
- :func:`ssl` and :class:`sslerror`
- :func:`socket.getaddrinfo`
- :func:`socket.gethostbyname`
- It is possible to disable dns patching by passing ``dns=False`` to :func:`patch_socket` of :func:`patch_all`
- If ssl is not available (Python < 2.6 without ``ssl`` package installed) then :func:`ssl` is removed from the target :mod:`socket` module.
* :mod:`ssl` module -- :func:`patch_ssl`
- :class:`SSLSocket`
- :func:`wrap_socket`
- :func:`get_server_certificate`
- :func:`sslwrap_simple`
* :mod:`os` module -- :func:`patch_os`
- :func:`fork`
* :mod:`time` module -- :func:`patch_time`
- :func:`sleep`
* :mod:`select` module -- :func:`patch_select`
- :func:`select`
- Removes polling mechanisms that :mod:`gevent.select` does not simulate: poll, epoll, kqueue, kevent
* :mod:`thread` and :mod:`threading` modules -- :func:`patch_thread`
- Become greenlet-based.
- :func:`get_ident`
- :func:`start_new_thread`
- :class:`LockType`
- :func:`allocate_lock`
- :func:`exit`
- :func:`stack_size`
- thread-local storage becomes greenlet-local storage
"""
from __future__ import absolute_import from __future__ import absolute_import
import sys import sys
from sys import version_info
__all__ = ['patch_all', __all__ = ['patch_all',
'patch_socket', 'patch_socket',
...@@ -65,7 +10,9 @@ __all__ = ['patch_all', ...@@ -65,7 +10,9 @@ __all__ = ['patch_all',
'patch_os', 'patch_os',
'patch_time', 'patch_time',
'patch_select', 'patch_select',
'patch_thread'] 'patch_thread',
'patch_subprocess',
'patch_sys']
# maps module name -> attribute name -> original item # maps module name -> attribute name -> original item
...@@ -123,6 +70,19 @@ def patch_module(name, items=None): ...@@ -123,6 +70,19 @@ def patch_module(name, items=None):
patch_item(module, attr, getattr(gevent_module, attr)) patch_item(module, attr, getattr(gevent_module, attr))
def _patch_sys_std(name):
from gevent.fileobject import FileObjectThread
orig = getattr(sys, name)
if not isinstance(orig, FileObjectThread):
patch_item(sys, name, FileObjectThread(orig))
def patch_sys(stdin=True, stdout=True, stderr=True):
_patch_sys_std('stdin')
_patch_sys_std('stdout')
_patch_sys_std('stderr')
def patch_os(): def patch_os():
"""Replace :func:`os.fork` with :func:`gevent.fork`. Does nothing if fork is not available.""" """Replace :func:`os.fork` with :func:`gevent.fork`. Does nothing if fork is not available."""
patch_module('os') patch_module('os')
...@@ -215,9 +175,11 @@ def patch_subprocess(): ...@@ -215,9 +175,11 @@ def patch_subprocess():
def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, httplib=False, def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, httplib=False,
subprocess=True, aggressive=True, Event=False): subprocess=True, sys=False, aggressive=True, Event=False):
"""Do all of the default monkey patching (calls every other function in this module.""" """Do all of the default monkey patching (calls every other function in this module."""
# order is important # order is important
if sys:
patch_sys()
if os: if os:
patch_os() patch_os()
if time: if time:
...@@ -229,12 +191,13 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru ...@@ -229,12 +191,13 @@ def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=Tru
if select: if select:
patch_select(aggressive=aggressive) patch_select(aggressive=aggressive)
if ssl: if ssl:
try: if version_info[:2] > (2, 5):
patch_ssl() patch_ssl()
except ImportError: else:
if sys.version_info[:2] > (2, 5): try:
raise patch_ssl()
# in Python 2.5, 'ssl' is a standalone package not included in stdlib except ImportError:
pass # in Python 2.5, 'ssl' is a standalone package not included in stdlib
if httplib: if httplib:
raise ValueError('gevent.httplib is no longer provided, httplib must be False') raise ValueError('gevent.httplib is no longer provided, httplib must be False')
if subprocess: if subprocess:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment