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

Python 向运行gevent pywsgi.WSGIServer添加更多应用程序,python,wsgi,gevent,Python,Wsgi,Gevent,我有一个如下所示的应用程序: application = DirectoryApp( 'app/static', index_page='index.html', hide_index_with_redirect=True) if __name__ == '__main__': config = get_config()

我有一个如下所示的应用程序:

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()
问题是,通过这样做,它永远不会进入我放在应用程序堆栈顶部的新应用程序类的_调用


在我看来,我似乎没有影响服务器实际使用的堆栈…

您可以使用paste的级联加载多个wsgi应用程序:

下面是将其与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()

这不是我真正想要的。。。请再读一遍这篇文章。我不相信有办法做到这一点。WSGI设计为一次运行一个应用程序。可能还有很多其他的方法来实现您想要的,如果您使用的是Django、Pyramid或Flask之类的框架,那么还有其他的模式,比如Flask的蓝图。