diff --git a/CHANGES/2988.feature b/CHANGES/2988.feature new file mode 100644 index 00000000000..1e1fd0a5aeb --- /dev/null +++ b/CHANGES/2988.feature @@ -0,0 +1 @@ +Added a ``GunicornWebWorker`` feature for extending the aiohttp server configuration by allowing the 'wsgi' coroutine to return ``web.AppRunner`` object. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 223f006534a..269e0786280 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -83,6 +83,7 @@ David Bibb David Michael Brown Denilson Amorim Denis Matiychuk +Denis Moshensky Dennis Kliban Dima Veselov Dimitar Dimitrov diff --git a/aiohttp/worker.py b/aiohttp/worker.py index 7d70f2ad15c..9b89c2044d8 100644 --- a/aiohttp/worker.py +++ b/aiohttp/worker.py @@ -64,23 +64,33 @@ 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