2016-04-17 45 views
1

我想了解如何使這個example採取aiohttp.web.Application實例,以便它可以使用這個模式:使用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。 初始化無法弄清楚如何修改啓動方式(我只保留了我需要幫助修改部分):

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

我發現,我想你可能會感興趣:

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

其餘保持不變。