1

我试图了解如何使这个例子成为一个aiohttp.web.Application实例,以便它可以使用这种模式:

def handler1(request):
    ...

def handler2(request):
    ...

app = web.Application()
app.router.add_route('GET', '/one', handler1)
app.router.add_route('GET', '/two', handler2)

让我的生活变得困难的是,我已经能够将我的应用程序实例带到 ChildProcess。init无法弄清楚如何修改启动方法(我只保留了需要帮助修改的部分):

class ChildProcess:

    def __init__(self, up_read, down_write, app, args, sock):
        ...
        self.app = app
        ...

    def start(self):
        ...

        # how to leverage the app.router here????
        # these few lines like aiohttp.web.run_app(app) code
        # there must be a way to make this work together
        f = loop.create_server(
            lambda: HttpRequestHandler(debug=True, keep_alive=75),
            sock=self.sock)

        srv = loop.run_until_complete(f)
4

1 回答 1

1

我发现并且我认为您可能会感兴趣:

class ChildProcess:

    def start(self):
        ...
        # lines 123, 124, and 125 become:
        handler = web.RequestHandlerFactory(self.app, self.app.router, loop=loop,
                                            debug=True, keep_alive=75)

        f = loop.create_server(lambda: handler(), sock=self.sock)
        ...

其余的保持不变。

于 2016-04-17T13:03:03.123 回答