add ensure_future method
This commit is contained in:
parent
75fca1b9c7
commit
94b2352c2c
|
@ -1,7 +1,7 @@
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import warnings
|
import warnings
|
||||||
from asyncio import get_event_loop
|
from asyncio import get_event_loop, ensure_future
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from inspect import isawaitable, stack, getmodulename
|
from inspect import isawaitable, stack, getmodulename
|
||||||
|
@ -44,7 +44,7 @@ class Sanic:
|
||||||
self._blueprint_order = []
|
self._blueprint_order = []
|
||||||
self.debug = None
|
self.debug = None
|
||||||
self.sock = None
|
self.sock = None
|
||||||
self.processes = None
|
self.before_start = []
|
||||||
|
|
||||||
# Register alternative method names
|
# Register alternative method names
|
||||||
self.go_fast = self.run
|
self.go_fast = self.run
|
||||||
|
@ -53,6 +53,23 @@ class Sanic:
|
||||||
# Registration
|
# Registration
|
||||||
# -------------------------------------------------------------------- #
|
# -------------------------------------------------------------------- #
|
||||||
|
|
||||||
|
def ensure_future(self, task):
|
||||||
|
"""
|
||||||
|
Schedule a task to run later, after the loop has started.
|
||||||
|
Different from asyncio.ensure_future in that it does not
|
||||||
|
also return a future, and the actual ensure_future call
|
||||||
|
is delayed until before server start.
|
||||||
|
|
||||||
|
:param task: A future, couroutine or awaitable.
|
||||||
|
"""
|
||||||
|
def run(app, loop):
|
||||||
|
if callable(task):
|
||||||
|
ensure_future(task())
|
||||||
|
else:
|
||||||
|
ensure_future(task)
|
||||||
|
|
||||||
|
self.before_start.append(run)
|
||||||
|
|
||||||
# Decorator
|
# Decorator
|
||||||
def route(self, uri, methods=frozenset({'GET'}), host=None):
|
def route(self, uri, methods=frozenset({'GET'}), host=None):
|
||||||
"""
|
"""
|
||||||
|
@ -397,6 +414,13 @@ class Sanic:
|
||||||
:param protocol: Subclass of asyncio protocol class
|
:param protocol: Subclass of asyncio protocol class
|
||||||
:return: Nothing
|
:return: Nothing
|
||||||
"""
|
"""
|
||||||
|
if before_start is not None:
|
||||||
|
if not isinstance(before_start, list):
|
||||||
|
before_start = [before_start]
|
||||||
|
before_start.extend(self.before_start)
|
||||||
|
else:
|
||||||
|
before_start = self.before_start
|
||||||
|
|
||||||
server_settings = self._helper(
|
server_settings = self._helper(
|
||||||
host=host, port=port, debug=debug, before_start=before_start,
|
host=host, port=port, debug=debug, before_start=before_start,
|
||||||
after_start=after_start, before_stop=before_stop,
|
after_start=after_start, before_stop=before_stop,
|
||||||
|
|
30
tests/test_ensure_future.py
Normal file
30
tests/test_ensure_future.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
import sanic
|
||||||
|
from sanic.utils import sanic_endpoint_test
|
||||||
|
from sanic.response import text
|
||||||
|
from threading import Event
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
def test_ensure_future():
|
||||||
|
e = Event()
|
||||||
|
async def coro():
|
||||||
|
await asyncio.sleep(0.05)
|
||||||
|
e.set()
|
||||||
|
|
||||||
|
app = sanic.Sanic()
|
||||||
|
app.ensure_future(coro)
|
||||||
|
|
||||||
|
@app.route('/early')
|
||||||
|
def not_set(request):
|
||||||
|
return text(e.is_set())
|
||||||
|
|
||||||
|
@app.route('/late')
|
||||||
|
async def set(request):
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
return text(e.is_set())
|
||||||
|
|
||||||
|
|
||||||
|
request, response = sanic_endpoint_test(app, uri='/early')
|
||||||
|
assert response.body == b'False'
|
||||||
|
|
||||||
|
request, response = sanic_endpoint_test(app, uri='/late')
|
||||||
|
assert response.body == b'True'
|
Loading…
Reference in New Issue
Block a user