set wsgi.multithread to True for async workers

Also simplifies the environment handling in the gevent_pywsgi
server so that it also has this key. An added side effect is
that the gunicorn FileWrapper gets set for the gevent_pywsgi
worker, too.

Fixes #486
This commit is contained in:
Randall Leeds 2013-06-22 17:14:50 -07:00 committed by benoitc
parent 640bf42206
commit 56b5f4038f
3 changed files with 21 additions and 16 deletions

View File

@ -67,22 +67,29 @@ class WSGIErrorsWraper(io.RawIOBase):
stream.flush()
def default_environ(req, sock, cfg):
def base_environ(cfg):
return {
"wsgi.input": req.body,
"wsgi.errors": WSGIErrorsWraper(cfg),
"wsgi.version": (1, 0),
"wsgi.multithread": False,
"wsgi.multiprocess": (cfg.workers > 1),
"wsgi.run_once": False,
"wsgi.file_wrapper": FileWrapper,
"gunicorn.socket": sock,
"SERVER_SOFTWARE": SERVER_SOFTWARE,
}
def default_environ(req, sock, cfg):
env = base_environ(cfg)
env.update({
"wsgi.input": req.body,
"gunicorn.socket": sock,
"REQUEST_METHOD": req.method,
"QUERY_STRING": req.query,
"RAW_URI": req.uri,
"SERVER_PROTOCOL": "HTTP/%s" % ".".join([str(v) for v in req.version])
}
})
return env
def proxy_environ(req):

View File

@ -81,6 +81,7 @@ class AsyncWorker(base.Worker):
self.cfg.pre_request(self, req)
resp, environ = wsgi.create(req, sock, addr,
listener.getsockname(), self.cfg)
environ["wsgi.multithread"] = True
self.nr += 1
if self.alive and self.nr >= self.max_requests:
self.log.info("Autorestarting worker after current request.")

View File

@ -28,6 +28,7 @@ from gevent.socket import wait_write, socket
from gevent import pywsgi
import gunicorn
from gunicorn.http.wsgi import base_environ
from gunicorn.workers.async import AsyncWorker
from gunicorn.http.wsgi import sendfile as o_sendfile
@ -49,16 +50,6 @@ def patch_sendfile():
if o_sendfile is not None:
setattr(wsgi, "sendfile", _gevent_sendfile)
BASE_WSGI_ENV = {
'GATEWAY_INTERFACE': 'CGI/1.1',
'SERVER_SOFTWARE': VERSION,
'SCRIPT_NAME': '',
'wsgi.version': (1, 0),
'wsgi.multithread': False,
'wsgi.multiprocess': False,
'wsgi.run_once': False
}
class GeventWorker(AsyncWorker):
@ -106,9 +97,15 @@ class GeventWorker(AsyncWorker):
s.setblocking(1)
pool = Pool(self.worker_connections)
if self.server_class is not None:
environ = base_environ(self.cfg)
environ.update({
"wsgi.multithread": True,
"SERVER_SOFTWARE": VERSION,
})
server = self.server_class(
s, application=self.wsgi, spawn=pool, log=self.log,
handler_class=self.wsgi_handler, **ssl_args)
handler_class=self.wsgi_handler, environ=environ,
**ssl_args)
else:
hfun = partial(self.handle, s)
server = StreamServer(s, handle=hfun, spawn=pool, **ssl_args)
@ -231,7 +228,7 @@ class PyWSGIHandler(pywsgi.WSGIHandler):
class PyWSGIServer(pywsgi.WSGIServer):
base_env = BASE_WSGI_ENV
pass
class GeventPyWSGIWorker(GeventWorker):