From c7a0af5d3a1603de22d1083bc6da1af1dc24256d Mon Sep 17 00:00:00 2001 From: benoitc Date: Tue, 21 Feb 2012 16:44:26 +0100 Subject: [PATCH] make the django monkey patching less intrusive. only patch in the `run_gunicorn` command. --- gunicorn/management/commands/run_gunicorn.py | 31 +++++++++++++++ gunicorn/monkey.py | 40 -------------------- gunicorn/workers/geventlet.py | 2 - gunicorn/workers/ggevent.py | 3 -- 4 files changed, 31 insertions(+), 45 deletions(-) delete mode 100644 gunicorn/monkey.py diff --git a/gunicorn/management/commands/run_gunicorn.py b/gunicorn/management/commands/run_gunicorn.py index 47ef80c9..da3bf84b 100644 --- a/gunicorn/management/commands/run_gunicorn.py +++ b/gunicorn/management/commands/run_gunicorn.py @@ -10,6 +10,37 @@ from django.core.management.base import BaseCommand, CommandError from gunicorn.app.djangoapp import DjangoApplicationCommand from gunicorn.config import make_settings +# monkey patch django. +# This patch make sure that we use real threads to get the ident which +# is going to happen if we are using gevent or eventlet. +try: + from django.db.backends import BaseDatabaseWrapper, DatabaseError + + if "validate_thread_sharing" in BaseDatabaseWrapper.__dict__: + import thread + _get_ident = thread.get_ident + + __old__init__ = BaseDatabaseWrapper.__init__ + + def _init(self, *args, **kwargs): + __old__init__(self, *args, **kwargs) + self._thread_ident = _get_ident() + + def _validate_thread_sharing(self): + if (not self.allow_thread_sharing + and self._thread_ident != _get_ident()): + raise DatabaseError("DatabaseWrapper objects created in a " + "thread can only be used in that same thread. The object " + "with alias '%s' was created in thread id %s and this is " + "thread id %s." + % (self.alias, self._thread_ident, _get_ident())) + + BaseDatabaseWrapper.__init__ = _init + BaseDatabaseWrapper.validate_thread_sharing = _validate_thread_sharing +except ImportError: + pass + + def make_options(): g_settings = make_settings(ignore=("version")) diff --git a/gunicorn/monkey.py b/gunicorn/monkey.py deleted file mode 100644 index d0e31d50..00000000 --- a/gunicorn/monkey.py +++ /dev/null @@ -1,40 +0,0 @@ -# -*- coding: utf-8 - -# -# This file is part of gunicorn released under the MIT license. -# See the NOTICE for more information. - -def patch_django(): - """ monkey patch django. - - This patch make sure that we use real threads to get the ident which - is going to happen if we are using gevent or eventlet. - """ - - try: - from django.db.backends import BaseDatabaseWrapper, DatabaseError - - if "validate_thread_sharing" in BaseDatabaseWrapper.__dict__: - import thread - _get_ident = thread.get_ident - - __old__init__ = BaseDatabaseWrapper.__init__ - - def _init(self, *args, **kwargs): - __old__init__(self, *args, **kwargs) - self._thread_ident = _get_ident() - - def _validate_thread_sharing(self): - if (not self.allow_thread_sharing - and self._thread_ident != _get_ident()): - raise DatabaseError("DatabaseWrapper objects created in a " - "thread can only be used in that same thread. The object " - "with alias '%s' was created in thread id %s and this is " - "thread id %s." - % (self.alias, self._thread_ident, _get_ident())) - - BaseDatabaseWrapper.__init__ = _init - BaseDatabaseWrapper.validate_thread_sharing = _validate_thread_sharing - - except ImportError: - pass - diff --git a/gunicorn/workers/geventlet.py b/gunicorn/workers/geventlet.py index bd30d2fa..e020ca4b 100644 --- a/gunicorn/workers/geventlet.py +++ b/gunicorn/workers/geventlet.py @@ -14,7 +14,6 @@ except ImportError: from eventlet import hubs from eventlet.greenio import GreenSocket -from gunicorn.monkey import patch_django from gunicorn.workers.async import AsyncWorker class EventletWorker(AsyncWorker): @@ -24,7 +23,6 @@ class EventletWorker(AsyncWorker): import eventlet if eventlet.version_info < (0,9,7): raise RuntimeError("You need eventlet >= 0.9.7") - patch_django() eventlet.monkey_patch(os=False) def init_process(self): diff --git a/gunicorn/workers/ggevent.py b/gunicorn/workers/ggevent.py index 827a66e6..0be159b6 100644 --- a/gunicorn/workers/ggevent.py +++ b/gunicorn/workers/ggevent.py @@ -22,7 +22,6 @@ from gevent.server import StreamServer from gevent import pywsgi import gunicorn -from gunicorn.monkey import patch_django from gunicorn.workers.async import AsyncWorker VERSION = "gevent/%s gunicorn/%s" % (gevent.__version__, gunicorn.__version__) @@ -46,10 +45,8 @@ class GeventWorker(AsyncWorker): def setup(cls): from gevent import monkey monkey.noisy = False - patch_django() monkey.patch_all() - def timeout_ctx(self): return gevent.Timeout(self.cfg.keepalive, False)