From 8e3f3977bd456832eb731b3136c798593d2bd59e Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Thu, 21 Dec 2017 23:27:34 -0800 Subject: [PATCH] allow add_task after server starts --- sanic/app.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 23d404f1..5b941e44 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -86,12 +86,18 @@ class Sanic: :param task: future, couroutine or awaitable """ - @self.listener('before_server_start') - def run(app, loop): + try: if callable(task): - loop.create_task(task()) + self.loop.create_task(task()) else: - loop.create_task(task) + self.loop.create_task(task) + except SanicException: + @self.listener('before_server_start') + def run(app, loop): + if callable(task): + loop.create_task(task()) + else: + loop.create_task(task) # Decorator def listener(self, event):