Mergeback of 21.12.1 (#2358)
Co-authored-by: Néstor Pérez <25409753+prryplatypus@users.noreply.github.com> Co-authored-by: Ryu juheon <saidbysolo@gmail.com>
This commit is contained in:
58
sanic/app.py
58
sanic/app.py
@@ -1552,10 +1552,19 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
name: Optional[str] = None,
|
||||
register: bool = True,
|
||||
) -> Task:
|
||||
prepped = cls._prep_task(task, app, loop)
|
||||
task = loop.create_task(prepped, name=name)
|
||||
if not isinstance(task, Future):
|
||||
prepped = cls._prep_task(task, app, loop)
|
||||
if sys.version_info < (3, 8):
|
||||
if name:
|
||||
error_logger.warning(
|
||||
"Cannot set a name for a task when using Python 3.7. "
|
||||
"Your task will be created without a name."
|
||||
)
|
||||
task = loop.create_task(prepped)
|
||||
else:
|
||||
task = loop.create_task(prepped, name=name)
|
||||
|
||||
if name and register:
|
||||
if name and register and sys.version_info > (3, 7):
|
||||
app._task_registry[name] = task
|
||||
|
||||
return task
|
||||
@@ -1617,10 +1626,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
def get_task(
|
||||
self, name: str, *, raise_exception: bool = True
|
||||
) -> Optional[Task]:
|
||||
if sys.version_info == (3, 7):
|
||||
raise RuntimeError(
|
||||
"This feature is only supported on using Python 3.8+."
|
||||
if sys.version_info < (3, 8):
|
||||
error_logger.warning(
|
||||
"This feature (get_task) is only supported on using "
|
||||
"Python 3.8+."
|
||||
)
|
||||
return
|
||||
try:
|
||||
return self._task_registry[name]
|
||||
except KeyError:
|
||||
@@ -1637,10 +1648,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
*,
|
||||
raise_exception: bool = True,
|
||||
) -> None:
|
||||
if sys.version_info == (3, 7):
|
||||
raise RuntimeError(
|
||||
"This feature is only supported on using Python 3.8+."
|
||||
if sys.version_info < (3, 8):
|
||||
error_logger.warning(
|
||||
"This feature (cancel_task) is only supported on using "
|
||||
"Python 3.8+."
|
||||
)
|
||||
return
|
||||
task = self.get_task(name, raise_exception=raise_exception)
|
||||
if task and not task.cancelled():
|
||||
args: Tuple[str, ...] = ()
|
||||
@@ -1659,10 +1672,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
...
|
||||
|
||||
def purge_tasks(self):
|
||||
if sys.version_info == (3, 7):
|
||||
raise RuntimeError(
|
||||
"This feature is only supported on using Python 3.8+."
|
||||
if sys.version_info < (3, 8):
|
||||
error_logger.warning(
|
||||
"This feature (purge_tasks) is only supported on using "
|
||||
"Python 3.8+."
|
||||
)
|
||||
return
|
||||
for task in self.tasks:
|
||||
if task.done() or task.cancelled():
|
||||
name = task.get_name()
|
||||
@@ -1675,10 +1690,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
def shutdown_tasks(
|
||||
self, timeout: Optional[float] = None, increment: float = 0.1
|
||||
):
|
||||
if sys.version_info == (3, 7):
|
||||
raise RuntimeError(
|
||||
"This feature is only supported on using Python 3.8+."
|
||||
if sys.version_info < (3, 8):
|
||||
error_logger.warning(
|
||||
"This feature (shutdown_tasks) is only supported on using "
|
||||
"Python 3.8+."
|
||||
)
|
||||
return
|
||||
for task in self.tasks:
|
||||
task.cancel()
|
||||
|
||||
@@ -1692,10 +1709,12 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
|
||||
@property
|
||||
def tasks(self):
|
||||
if sys.version_info == (3, 7):
|
||||
raise RuntimeError(
|
||||
"This feature is only supported on using Python 3.8+."
|
||||
if sys.version_info < (3, 8):
|
||||
error_logger.warning(
|
||||
"This feature (tasks) is only supported on using "
|
||||
"Python 3.8+."
|
||||
)
|
||||
return
|
||||
return iter(self._task_registry.values())
|
||||
|
||||
# -------------------------------------------------------------------- #
|
||||
@@ -1709,7 +1728,8 @@ class Sanic(BaseSanic, metaclass=TouchUpMeta):
|
||||
details: https://asgi.readthedocs.io/en/latest
|
||||
"""
|
||||
self.asgi = True
|
||||
self.motd("")
|
||||
if scope["type"] == "lifespan":
|
||||
self.motd("")
|
||||
self._asgi_app = await ASGIApp.create(self, scope, receive, send)
|
||||
asgi_app = self._asgi_app
|
||||
await asgi_app()
|
||||
|
||||
@@ -124,22 +124,27 @@ class Config(dict, metaclass=DescriptorMeta):
|
||||
raise AttributeError(f"Config has no '{ke.args[0]}'")
|
||||
|
||||
def __setattr__(self, attr, value) -> None:
|
||||
if attr in self.__class__.__setters__:
|
||||
try:
|
||||
super().__setattr__(attr, value)
|
||||
except AttributeError:
|
||||
...
|
||||
else:
|
||||
return None
|
||||
self.update({attr: value})
|
||||
|
||||
def __setitem__(self, attr, value) -> None:
|
||||
self.update({attr: value})
|
||||
|
||||
def update(self, *other, **kwargs) -> None:
|
||||
other_mapping = {k: v for item in other for k, v in dict(item).items()}
|
||||
super().update(*other, **kwargs)
|
||||
for attr, value in {**other_mapping, **kwargs}.items():
|
||||
kwargs.update({k: v for item in other for k, v in dict(item).items()})
|
||||
setters: Dict[str, Any] = {
|
||||
k: kwargs.pop(k)
|
||||
for k in {**kwargs}.keys()
|
||||
if k in self.__class__.__setters__
|
||||
}
|
||||
|
||||
for key, value in setters.items():
|
||||
try:
|
||||
super().__setattr__(key, value)
|
||||
except AttributeError:
|
||||
...
|
||||
|
||||
super().update(**kwargs)
|
||||
for attr, value in {**setters, **kwargs}.items():
|
||||
self._post_set(attr, value)
|
||||
|
||||
def _post_set(self, attr, value) -> None:
|
||||
|
||||
Reference in New Issue
Block a user