添加更多应用程序以运行gevent pywsgi.WSGIServer

时间:2014-07-11 14:05:22

标签: python wsgi gevent

我的应用程序看起来像这样:

application = DirectoryApp(
                           'app/static',
                           index_page='index.html',
                           hide_index_with_redirect=True)

if __name__ == '__main__':
    config = get_config()

    from gevent import pywsgi
    server = pywsgi.WSGIServer((config['host'], config['port']), application)
    server.serve_forever()

在服务器启动后,是否可以使用代码将另一个应用程序添加到堆栈中?我想要的是能够做一些事情:

# Create new application class
class AnotherController((object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        from webob import Request, Response
        req = Request(environ)

        if req.method == 'GET' and req.path_info.startswith('/anothercontroller'):
            res = Response(
                body='Hello World, Another Controller!',
                status=200,
                content_type='text/plain'
            )

            return res(environ, start_response)

        return self.app(environ, start_response)

def add_application():
    global application
    application = AnotherController(application)

# Add the application to the stack after the fact it is already running
add_application()

问题在于,通过这样做,它永远不会进入我放在应用程序堆栈之上的新应用程序类的__call__

对我而言,似乎我并没有影响服务器实际使用的堆栈......

1 个答案:

答案 0 :(得分:-1)

您可以使用use paste的级联来加载多个wsgi应用: http://pythonpaste.org/modules/cascade.html

以下是使用gevent.pywsgi的代码示例:

def http_server():
    static_app = paste.urlparser.StaticURLParser(
        os.path.join(twit.__path__[0], "static"))
    apps = paste.cascade.Cascade([static_app, search.app])
    http_server = gevent.pywsgi.WSGIServer(
        ('', 8000), apps)
    http_server.start()