From dc5d682448e3f6595bdca5cb764e5f26ca29e295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Sat, 21 Mar 2020 20:42:35 +0200 Subject: [PATCH] app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests --- sanic/app.py | 23 +++++++++++++++-------- sanic/testing.py | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 3b93b3745e..a8a7ca003b 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -1096,6 +1096,8 @@ def run( stop_event: Any = None, register_sys_signals: bool = True, access_log: Optional[bool] = None, + auto_reload = None, + asyncio_server_kwargs = None, **kwargs: Any, ) -> None: """Run the HTTP Server and listen until keyboard interrupt or term @@ -1126,6 +1128,9 @@ def run( :type register_sys_signals: bool :param access_log: Enables writing access logs (slows server) :type access_log: bool + :param asyncio_server_kwargs: key-value arguments for + asyncio/uvloop create_server method + :type asyncio_server_kwargs: dict :return: Nothing """ if "loop" in kwargs: @@ -1135,14 +1140,14 @@ def run( "https://sanic.readthedocs.io/en/latest/sanic/deploying.html" "#asynchronous-support" ) - - # Default auto_reload to false - auto_reload = False - # If debug is set, default it to true (unless on windows) - if debug and os.name == "posix": - auto_reload = True - # Allow for overriding either of the defaults - auto_reload = kwargs.get("auto_reload", auto_reload) + if kwargs: + logger.warning(f"Sanic.run ignored arguments {kwargs.keys()}") + if auto_reload is None: + # Default auto_reload to false + auto_reload = False + # If debug is set, default it to true (unless on windows) + if debug and os.name == "posix": + auto_reload = True if sock is None: host, port = host or "127.0.0.1", port or 8000 @@ -1174,6 +1179,8 @@ def run( register_sys_signals=register_sys_signals, auto_reload=auto_reload, ) + if asyncio_server_kwargs: + server_settings["asyncio_server_kwargs"] = asyncio_server_kwargs try: self.is_running = True diff --git a/sanic/testing.py b/sanic/testing.py index df1194fe18..ba5e53f6af 100644 --- a/sanic/testing.py +++ b/sanic/testing.py @@ -97,7 +97,7 @@ async def error_handler(request, exception): server_kwargs = dict( host=host or self.host, port=self.port, - reuse_port=True, # Try to avoid test failures on Windows + asyncio_server_kwargs=dict(reuse_address=True), **server_kwargs, ) host, port = host or self.host, self.port