使用web.Application与多处理

时间:2016-04-17 12:33:02

标签: python-3.5 aiohttp

我正在尝试了解如何使example获取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)

1 个答案:

答案 0 :(得分: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)
        ...

其余的保持不变。