From a1ffc6d55b6d3fa5da175eb65d9c4cce58d2a81e Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Thu, 21 Dec 2017 17:12:31 -0800 Subject: [PATCH] try to inject the app in add_task method --- sanic/app.py | 10 ++++++++-- tests/test_create_task.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 5b941e44..26ef83b0 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -88,14 +88,20 @@ class Sanic: """ try: if callable(task): - self.loop.create_task(task()) + try: + self.loop.create_task(task(self)) + except TypeError: + self.loop.create_task(task()) else: self.loop.create_task(task) except SanicException: @self.listener('before_server_start') def run(app, loop): if callable(task): - loop.create_task(task()) + try: + loop.create_task(task(self)) + except TypeError: + loop.create_task(task()) else: loop.create_task(task) diff --git a/tests/test_create_task.py b/tests/test_create_task.py index d7418466..1517ca8c 100644 --- a/tests/test_create_task.py +++ b/tests/test_create_task.py @@ -2,6 +2,7 @@ from sanic import Sanic from sanic.response import text from threading import Event import asyncio +from queue import Queue def test_create_task(): @@ -28,3 +29,19 @@ def test_create_task(): request, response = app.test_client.get('/late') assert response.body == b'True' + +def test_create_task_with_app_arg(): + app = Sanic('test_add_task') + q = Queue() + + @app.route('/') + def not_set(request): + return "hello" + + async def coro(app): + q.put(app.name) + + app.add_task(coro) + + request, response = app.test_client.get('/') + assert q.get() == 'test_add_task'