From 161c8c03cb505d64b0180ac1089435a11392e63b Mon Sep 17 00:00:00 2001 From: benoitc Date: Tue, 21 Feb 2012 14:24:55 +0100 Subject: [PATCH] monkey patch django. close #293 . 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. --- gunicorn/app/django_wsgi.py | 1 + gunicorn/management/commands/run_gunicorn.py | 3 --- gunicorn/workers/async.py | 2 +- gunicorn/workers/geventlet.py | 2 ++ gunicorn/workers/ggevent.py | 4 +++- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gunicorn/app/django_wsgi.py b/gunicorn/app/django_wsgi.py index dbf2f331..cc617409 100644 --- a/gunicorn/app/django_wsgi.py +++ b/gunicorn/app/django_wsgi.py @@ -29,6 +29,7 @@ except ImportError: from gunicorn import util + def make_wsgi_application(): # validate models s = StringIO() diff --git a/gunicorn/management/commands/run_gunicorn.py b/gunicorn/management/commands/run_gunicorn.py index 828716d6..19239abd 100644 --- a/gunicorn/management/commands/run_gunicorn.py +++ b/gunicorn/management/commands/run_gunicorn.py @@ -7,7 +7,6 @@ from optparse import make_option import sys from django.core.management.base import BaseCommand, CommandError -from django.conf import settings from gunicorn.app.djangoapp import DjangoApplicationCommand from gunicorn.config import make_settings @@ -67,7 +66,5 @@ class Command(BaseCommand): if addrport: options['bind'] = addrport - options['default_proc_name'] = settings.SETTINGS_MODULE - admin_media_path = options.pop('admin_media_path', '') DjangoApplicationCommand(options, admin_media_path).run() diff --git a/gunicorn/workers/async.py b/gunicorn/workers/async.py index f1f0d0ea..59e51943 100644 --- a/gunicorn/workers/async.py +++ b/gunicorn/workers/async.py @@ -61,7 +61,7 @@ class AsyncWorker(base.Worker): self.log.info("Autorestarting worker after current request.") resp.force_close() self.alive = False - respiter = self.wsgi(environ, resp.start_response) + respiter = self.app.wsgi()(environ, resp.start_response) if respiter == ALREADY_HANDLED: return False try: diff --git a/gunicorn/workers/geventlet.py b/gunicorn/workers/geventlet.py index e020ca4b..bd30d2fa 100644 --- a/gunicorn/workers/geventlet.py +++ b/gunicorn/workers/geventlet.py @@ -14,6 +14,7 @@ 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): @@ -23,6 +24,7 @@ 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 926846fd..6fc67228 100644 --- a/gunicorn/workers/ggevent.py +++ b/gunicorn/workers/ggevent.py @@ -22,6 +22,7 @@ from gevent.server import StreamServer from gevent import pywsgi import gunicorn +from gunicorn.monkey import patch_django from gunicorn.workers.async import AsyncWorker from gunicorn.workers.base import Worker @@ -46,6 +47,7 @@ class GeventWorker(AsyncWorker): def setup(cls): from gevent import monkey monkey.noisy = False + patch_django() monkey.patch_all() @@ -58,7 +60,7 @@ class GeventWorker(AsyncWorker): pool = Pool(self.worker_connections) if self.server_class is not None: server = self.server_class( - self.socket, application=self.wsgi, spawn=pool, log=self.log, + self.socket, application=self.app.wsgi, spawn=pool, log=self.log, handler_class=self.wsgi_handler) else: server = StreamServer(self.socket, handle=self.handle, spawn=pool)