mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
make the django monkey patching less intrusive.
only patch in the `run_gunicorn` command.
This commit is contained in:
parent
fa341c6531
commit
c7a0af5d3a
@ -10,6 +10,37 @@ from django.core.management.base import BaseCommand, CommandError
|
|||||||
from gunicorn.app.djangoapp import DjangoApplicationCommand
|
from gunicorn.app.djangoapp import DjangoApplicationCommand
|
||||||
from gunicorn.config import make_settings
|
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():
|
def make_options():
|
||||||
g_settings = make_settings(ignore=("version"))
|
g_settings = make_settings(ignore=("version"))
|
||||||
|
|
||||||
|
|||||||
@ -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
|
|
||||||
|
|
||||||
@ -14,7 +14,6 @@ except ImportError:
|
|||||||
from eventlet import hubs
|
from eventlet import hubs
|
||||||
from eventlet.greenio import GreenSocket
|
from eventlet.greenio import GreenSocket
|
||||||
|
|
||||||
from gunicorn.monkey import patch_django
|
|
||||||
from gunicorn.workers.async import AsyncWorker
|
from gunicorn.workers.async import AsyncWorker
|
||||||
|
|
||||||
class EventletWorker(AsyncWorker):
|
class EventletWorker(AsyncWorker):
|
||||||
@ -24,7 +23,6 @@ class EventletWorker(AsyncWorker):
|
|||||||
import eventlet
|
import eventlet
|
||||||
if eventlet.version_info < (0,9,7):
|
if eventlet.version_info < (0,9,7):
|
||||||
raise RuntimeError("You need eventlet >= 0.9.7")
|
raise RuntimeError("You need eventlet >= 0.9.7")
|
||||||
patch_django()
|
|
||||||
eventlet.monkey_patch(os=False)
|
eventlet.monkey_patch(os=False)
|
||||||
|
|
||||||
def init_process(self):
|
def init_process(self):
|
||||||
|
|||||||
@ -22,7 +22,6 @@ from gevent.server import StreamServer
|
|||||||
from gevent import pywsgi
|
from gevent import pywsgi
|
||||||
|
|
||||||
import gunicorn
|
import gunicorn
|
||||||
from gunicorn.monkey import patch_django
|
|
||||||
from gunicorn.workers.async import AsyncWorker
|
from gunicorn.workers.async import AsyncWorker
|
||||||
|
|
||||||
VERSION = "gevent/%s gunicorn/%s" % (gevent.__version__, gunicorn.__version__)
|
VERSION = "gevent/%s gunicorn/%s" % (gevent.__version__, gunicorn.__version__)
|
||||||
@ -46,10 +45,8 @@ class GeventWorker(AsyncWorker):
|
|||||||
def setup(cls):
|
def setup(cls):
|
||||||
from gevent import monkey
|
from gevent import monkey
|
||||||
monkey.noisy = False
|
monkey.noisy = False
|
||||||
patch_django()
|
|
||||||
monkey.patch_all()
|
monkey.patch_all()
|
||||||
|
|
||||||
|
|
||||||
def timeout_ctx(self):
|
def timeout_ctx(self):
|
||||||
return gevent.Timeout(self.cfg.keepalive, False)
|
return gevent.Timeout(self.cfg.keepalive, False)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user