diff --git a/examples/amending_request_object.py b/examples/amending_request_object.py index 2188b680..63f8d1d5 100644 --- a/examples/amending_request_object.py +++ b/examples/amending_request_object.py @@ -25,5 +25,5 @@ def key_exist_handler(request): return text("num does not exist in request") - -app.run(host="0.0.0.0", port=8000, debug=True) +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000, debug=True) diff --git a/examples/blueprint_middlware_execution_order.py b/examples/blueprint_middlware_execution_order.py index b858061a..b0448da8 100644 --- a/examples/blueprint_middlware_execution_order.py +++ b/examples/blueprint_middlware_execution_order.py @@ -50,4 +50,5 @@ def pop_handler(request): app.blueprint(bp, url_prefix="/bp") -app.run(host="0.0.0.0", port=8000, debug=True, auto_reload=False) +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000, debug=True, auto_reload=False) diff --git a/examples/blueprints.py b/examples/blueprints.py index 4109f485..e368573b 100644 --- a/examples/blueprints.py +++ b/examples/blueprints.py @@ -37,4 +37,5 @@ app.blueprint(blueprint) app.blueprint(blueprint2) app.blueprint(blueprint3) -app.run(host="0.0.0.0", port=9999, debug=True) +if __name__ == "__main__": + app.run(host="0.0.0.0", port=9999, debug=True) diff --git a/examples/http_redirect.py b/examples/http_redirect.py index 0632279f..34dc43ab 100644 --- a/examples/http_redirect.py +++ b/examples/http_redirect.py @@ -69,5 +69,5 @@ async def runner(app: Sanic, app_server: AsyncioServer): app.is_running = False app.is_stopping = True - -https.run(port=HTTPS_PORT, debug=True) +if __name__ == "__main__": + https.run(port=HTTPS_PORT, debug=True) diff --git a/examples/limit_concurrency.py b/examples/limit_concurrency.py index 37015817..e97f964a 100644 --- a/examples/limit_concurrency.py +++ b/examples/limit_concurrency.py @@ -39,4 +39,5 @@ async def test(request): return json(response) -app.run(host="0.0.0.0", port=8000, workers=2) +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000, workers=2) diff --git a/examples/override_logging.py b/examples/override_logging.py index c60098e7..ebc0e86f 100644 --- a/examples/override_logging.py +++ b/examples/override_logging.py @@ -20,4 +20,5 @@ def test(request): return text("hey") -app.run(host="0.0.0.0", port=8000) +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000) diff --git a/examples/request_timeout.py b/examples/request_timeout.py index 1c7885ee..07934b0c 100644 --- a/examples/request_timeout.py +++ b/examples/request_timeout.py @@ -20,4 +20,5 @@ def timeout(request, exception): return response.text("RequestTimeout from error_handler.", 408) -app.run(host="0.0.0.0", port=8000) +if __name__ == "__main__": + app.run(host="0.0.0.0", port=8000) diff --git a/examples/run_async_advanced.py b/examples/run_async_advanced.py index dc8d71c6..c782d49a 100644 --- a/examples/run_async_advanced.py +++ b/examples/run_async_advanced.py @@ -35,34 +35,34 @@ async def after_server_stop(app, loop): async def test(request): return response.json({"answer": "42"}) +if __name__ == "__main__": + asyncio.set_event_loop(uvloop.new_event_loop()) + serv_coro = app.create_server( + host="0.0.0.0", port=8000, return_asyncio_server=True + ) + loop = asyncio.get_event_loop() + serv_task = asyncio.ensure_future(serv_coro, loop=loop) + signal(SIGINT, lambda s, f: loop.stop()) + server: AsyncioServer = loop.run_until_complete(serv_task) + loop.run_until_complete(server.startup()) -asyncio.set_event_loop(uvloop.new_event_loop()) -serv_coro = app.create_server( - host="0.0.0.0", port=8000, return_asyncio_server=True -) -loop = asyncio.get_event_loop() -serv_task = asyncio.ensure_future(serv_coro, loop=loop) -signal(SIGINT, lambda s, f: loop.stop()) -server: AsyncioServer = loop.run_until_complete(serv_task) -loop.run_until_complete(server.startup()) + # When using app.run(), this actually triggers before the serv_coro. + # But, in this example, we are using the convenience method, even if it is + # out of order. + loop.run_until_complete(server.before_start()) + loop.run_until_complete(server.after_start()) + try: + loop.run_forever() + except KeyboardInterrupt: + loop.stop() + finally: + loop.run_until_complete(server.before_stop()) -# When using app.run(), this actually triggers before the serv_coro. -# But, in this example, we are using the convenience method, even if it is -# out of order. -loop.run_until_complete(server.before_start()) -loop.run_until_complete(server.after_start()) -try: - loop.run_forever() -except KeyboardInterrupt: - loop.stop() -finally: - loop.run_until_complete(server.before_stop()) + # Wait for server to close + close_task = server.close() + loop.run_until_complete(close_task) - # Wait for server to close - close_task = server.close() - loop.run_until_complete(close_task) - - # Complete all tasks on the loop - for connection in server.connections: - connection.close_if_idle() - loop.run_until_complete(server.after_stop()) + # Complete all tasks on the loop + for connection in server.connections: + connection.close_if_idle() + loop.run_until_complete(server.after_stop())