python - aiohttp lambda in loop.start_server() method -
i'm trying understand primer below:
if __name__ == '__main__': loop = asyncio.get_event_loop() f = loop.create_server( lambda: httprequesthandler(debug=true, keep_alive=75), '0.0.0.0', '8080') srv = loop.run_until_complete(f) print('serving on', srv.sockets[0].getsockname()) try: loop.run_forever() except keyboardinterrupt: pass
why there lambda nottation in asyncio.create_server method? asyncio docs, method accept class/function entry point. if attemt initiate class , pass initialized instance loop, im totally lost, because have strange behavion, init method of httprequesthandler invokes every request:
class httprequesthandler(aiohttp.server.serverhttpprotocol): def __init__(self): print(id(self))
what understood: classes httpserverprotocol parent callback, initialized every single request. save states between request, or process agregate logic need new object:
def process(): def init(self, loop): #instance variables self.loop=loop etc @asyncio.coroutine def serve(self): = asyncio.async(self.firstmethod(), loop=self.loop) b = asyncio.async(self.secondmethod(), loop=self.loop) yield asyncio.gather(*[a,b])
pass our logic class instance proto class , add serve coroutine method execution like: loop.run_until_complete(protoinstance.serve). works fine me.
Comments
Post a Comment