Skip to content

Commit

Permalink
make the django monkey patching less intrusive.
Browse files Browse the repository at this point in the history
only patch in the `run_gunicorn` command.
  • Loading branch information
benoitc committed Feb 21, 2012
1 parent fa341c6 commit c7a0af5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
31 changes: 31 additions & 0 deletions gunicorn/management/commands/run_gunicorn.py
Expand Up @@ -10,6 +10,37 @@
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"))

Expand Down
40 changes: 0 additions & 40 deletions gunicorn/monkey.py

This file was deleted.

2 changes: 0 additions & 2 deletions gunicorn/workers/geventlet.py
Expand Up @@ -14,7 +14,6 @@
from eventlet import hubs
from eventlet.greenio import GreenSocket

from gunicorn.monkey import patch_django
from gunicorn.workers.async import AsyncWorker

class EventletWorker(AsyncWorker):
Expand All @@ -24,7 +23,6 @@ def setup(cls):
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):
Expand Down
3 changes: 0 additions & 3 deletions gunicorn/workers/ggevent.py
Expand Up @@ -22,7 +22,6 @@
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__)
Expand All @@ -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)

Expand Down

0 comments on commit c7a0af5

Please sign in to comment.