DevLounge
DevLounge

Reputation: 8437

using web.Application with multiprocessing

I am trying to understand how to make this example take an aiohttp.web.Application instance so that it can use this pattern:

def handler1(request):
    ...

def handler2(request):
    ...

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

What makes my life difficult is that I have been able to bring my app instance up to the ChildProcess.init but cannot figure out how to modify the start method (I only kept the part I need help to modify):

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)

Upvotes: 1

Views: 275

Answers (1)

DevLounge
DevLounge

Reputation: 8437

I have found and I think you might be interested:

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)
        ...

The rest remains unchanged.

Upvotes: 1

Related Questions