From c8593374de937d1d597cda6e89a2d1de336aefe6 Mon Sep 17 00:00:00 2001 From: donovan Date: Sun, 20 Apr 2008 14:10:28 -0700 Subject: [PATCH] Use a couple of environment variables to figure out what wsgi app to really run in nginx_mod_wsgi.py. Now you can run any wsgi app you want easily in nginx. Also, delay calling start_response until the first hunk of the body is sent since nginx doesn't let us change the response codes or headers after the first time it is called. --- eventlet/support/nginx_mod_wsgi.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/eventlet/support/nginx_mod_wsgi.py b/eventlet/support/nginx_mod_wsgi.py index b7b7b06..1035680 100644 --- a/eventlet/support/nginx_mod_wsgi.py +++ b/eventlet/support/nginx_mod_wsgi.py @@ -1,5 +1,9 @@ import sys +import traceback + +sys.path.insert(0, '/Users/donovan/Code/mulib-hg') +sys.stdout = sys.stderr from eventlet import api from eventlet import httpc @@ -7,14 +11,15 @@ from eventlet import httpc from eventlet.hubs import nginx -def real_application(env, start_response): +def old_real_application(env, start_response): #result = httpc.get('http://127.0.0.1:8081/') start_response('200 OK', [('Content-type', 'text/plain')]) #sys.stderr.write("RESULT %r" % (result, )) - return 'hi' + return 'hello' def wrap_application(master, env, start_response): + real_application = api.named(env['eventlet_nginx_wsgi_app']) result = real_application(env, start_response) ## Should catch exception and return here? #sys.stderr.write("RESULT2 %r" % (result, )) @@ -22,6 +27,14 @@ def wrap_application(master, env, start_response): return None, None +class StartResponse(object): + def __init__(self, start_response): + self.start_response = start_response + + def __call__(self, *args): + self.args = args + + def application(env, start_response): hub = api.get_hub() @@ -34,17 +47,21 @@ def application(env, start_response): hub.current_application = api.getcurrent() slave = api.greenlet.greenlet(wrap_application) + response = StartResponse(start_response) result = slave.switch( - hub.current_application, env, start_response) + hub.current_application, env, response) while True: #sys.stderr.write("RESULT3 %r" % (result, )) if result is None or result == (None, None): yield '' else: + start_response(*response.args) if isinstance(result, tuple): - yield result[0] + for x in result[0]: + yield x else: - yield result + for x in result: + yield x return result = hub.switch()