From 174d928387fe2c754003ecdac08a18a93ec5e486 Mon Sep 17 00:00:00 2001 From: loven-doo <1429305+loven-doo@users.noreply.github.com> Date: Mon, 12 Apr 2021 18:43:36 +0300 Subject: [PATCH] coroutine is allowed to return web.AppRunner These changes allow the configuration of web.AppRunner object inside application code. This makes possible to use custom configs of Aiohttp server (like 'max_line_size' parameters setup) in pair with Gunicorn. Associated issue: https://github.com/aio-libs/aiohttp/issues/2988#ref-pullrequest-607852432 --- aiohttp/worker.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/aiohttp/worker.py b/aiohttp/worker.py index b164c801509..fc549cebec2 100644 --- a/aiohttp/worker.py +++ b/aiohttp/worker.py @@ -61,23 +61,31 @@ def run(self) -> None: sys.exit(self.exit_code) async def _run(self) -> None: + runner = None if isinstance(self.wsgi, Application): app = self.wsgi elif asyncio.iscoroutinefunction(self.wsgi): - app = await self.wsgi() + wsgi = await self.wsgi() + if isinstance(wsgi, web.AppRunner): + runner = wsgi + app = runner.app + else: + app = wsgi else: raise RuntimeError( "wsgi app should be either Application or " "async function returning Application, got {}".format(self.wsgi) ) - access_log = self.log.access_log if self.cfg.accesslog else None - runner = web.AppRunner( - app, - logger=self.log, - keepalive_timeout=self.cfg.keepalive, - access_log=access_log, - access_log_format=self._get_valid_log_format(self.cfg.access_log_format), - ) + + if runner is None: + access_log = self.log.access_log if self.cfg.accesslog else None + runner = web.AppRunner( + app, + logger=self.log, + keepalive_timeout=self.cfg.keepalive, + access_log=access_log, + access_log_format=self._get_valid_log_format(self.cfg.access_log_format), + ) await runner.setup() ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl else None