try to inject the app in add_task method
This commit is contained in:
parent
008cbe5ce7
commit
a1ffc6d55b
|
@ -88,6 +88,9 @@ class Sanic:
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
if callable(task):
|
if callable(task):
|
||||||
|
try:
|
||||||
|
self.loop.create_task(task(self))
|
||||||
|
except TypeError:
|
||||||
self.loop.create_task(task())
|
self.loop.create_task(task())
|
||||||
else:
|
else:
|
||||||
self.loop.create_task(task)
|
self.loop.create_task(task)
|
||||||
|
@ -95,6 +98,9 @@ class Sanic:
|
||||||
@self.listener('before_server_start')
|
@self.listener('before_server_start')
|
||||||
def run(app, loop):
|
def run(app, loop):
|
||||||
if callable(task):
|
if callable(task):
|
||||||
|
try:
|
||||||
|
loop.create_task(task(self))
|
||||||
|
except TypeError:
|
||||||
loop.create_task(task())
|
loop.create_task(task())
|
||||||
else:
|
else:
|
||||||
loop.create_task(task)
|
loop.create_task(task)
|
||||||
|
|
|
@ -2,6 +2,7 @@ from sanic import Sanic
|
||||||
from sanic.response import text
|
from sanic.response import text
|
||||||
from threading import Event
|
from threading import Event
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from queue import Queue
|
||||||
|
|
||||||
|
|
||||||
def test_create_task():
|
def test_create_task():
|
||||||
|
@ -28,3 +29,19 @@ def test_create_task():
|
||||||
|
|
||||||
request, response = app.test_client.get('/late')
|
request, response = app.test_client.get('/late')
|
||||||
assert response.body == b'True'
|
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'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user