Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove EasyRequest and ensure_async #14

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 0 additions & 104 deletions aeros/Request.py

This file was deleted.

42 changes: 6 additions & 36 deletions aeros/WebServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,13 @@
from .log import AccessLogFormatter, DefaultLogFormatter
from .caching.server import Cache
from .compression import Base
from .Request import EasyRequest

from functools import wraps
from asgiref.sync import sync_to_async, SyncToAsync
from typing import Callable, Coroutine, Any
from functools import wraps


def ensure_async(f: Callable, callback_on_sync: Callable = None):
# -> Callable[[Any], Coroutine[Any, Any, Any]]
"""
Ensures, that a supplied function is asynchronous. Detects Python-native async
functions and functions wrapped with `asgiref.sync.sync_to_async()`.

Arguments:
f (Callable): The function that should be ensured async
callback_on_sync (Callable): Called if a function is converted to async

Returns:
Callable[..., Coroutine[...]]: An async version of the supplied function
"""

#

if inspect.iscoroutinefunction(f) or type(f) == SyncToAsync:
return f

@wraps(f)
async def wrapper(*args, **kwargs):
return await sync_to_async(f)(*args, **kwargs)

if callback_on_sync:
callback_on_sync()
return wrapper


class WebServer(Quart):
""" This is the main server class which extends a standard Flask class by a bunch of features and major
performance improvements. It extends the quart class, which by itself is already an enhanced version of
Expand Down Expand Up @@ -202,7 +174,7 @@ def cache(self, timeout=None, key_prefix="view/%s", unless=None, forced_update=N

def decorator(f):
if self._cache is None:
print('No cache specified in', self)
self.logger.warning(f'No cache specified for {self.__class__.__name__} instance: "{self.name}"')
return f

@functools.wraps(f)
Expand Down Expand Up @@ -267,12 +239,10 @@ def route(self, *args, **kwargs):

def new_route_decorator(func):
try:
new_func = ensure_async(
func,
callback_on_sync=lambda: self.logger.warning(f'Endpoint function "{func.__name__}" for "{rule}" is not asynchronous.')
)
new_func = EasyRequest()(new_func) # inject EasyRequest into every function so that it is always available
Quart.route(self, *args, **kwargs)(new_func) # route the new function to Quart.route
if not (inspect.iscoroutinefunction(func) or type(func) == SyncToAsync):
self.logger.warning(f'Endpoint function "{func.__name__}" for "{rule}" is not asynchronous.')
Quart.route(self, *args, **kwargs)(func) # route the new function to Quart.route
return func
except Exception as e:
self.logger.critical(f'Endpoint function "{func.__name__}" for "{rule}" could not be routed: ' + str(e))
raise e
Expand Down
2 changes: 0 additions & 2 deletions aeros/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from .WebServer import WebServer
from .misc import *
from .Request import EasyRequest
from .thread import AdvancedThread
from .compression import Gzip, Br
from .caching import *
6 changes: 0 additions & 6 deletions aeros/misc.py

This file was deleted.