make the django monkey patching less intrusive.

only patch in the `run_gunicorn` command.
This commit is contained in:
benoitc 2012-02-21 16:44:26 +01:00
parent fa341c6531
commit c7a0af5d3a
4 changed files with 31 additions and 45 deletions

View File

@ -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"))

View File

@ -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

View File

@ -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):

View File

@ -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)