diff --git a/frameworks/Python/granian/app_asgi.py b/frameworks/Python/granian/app_asgi.py index f7cc2324175..35e09133777 100644 --- a/frameworks/Python/granian/app_asgi.py +++ b/frameworks/Python/granian/app_asgi.py @@ -55,8 +55,6 @@ async def pg_setup(): with Path('templates/fortune.html').open('r') as f: template = jinja2.Template(f.read()) -asyncio.get_event_loop().run_until_complete(pg_setup()) - def get_num_queries(scope): try: @@ -178,6 +176,26 @@ async def handle_404(scope, receive, send): } -def main(scope, receive, send): - handler = routes.get(scope['path'], handle_404) - return handler(scope, receive, send) +class App: + __slots__ = ["_handler"] + + def __init__(self): + self._handler = self._lifespan + + def __call__(self, scope, receive, send): + return self._handler(scope, receive, send) + + async def _lifespan(self, scope, receive, send): + if scope['type'] == 'lifespan': + message = await receive() + if message['type'] == 'lifespan.startup': + await pg_setup() + self._handler = self._asgi + await send({'type': 'lifespan.startup.complete'}) + + def _asgi(self, scope, receive, send): + handler = routes.get(scope['path'], handle_404) + return handler(scope, receive, send) + + +main = App() diff --git a/frameworks/Python/granian/app_rsgi.py b/frameworks/Python/granian/app_rsgi.py index dc1eeba7e2c..04a777460b1 100644 --- a/frameworks/Python/granian/app_rsgi.py +++ b/frameworks/Python/granian/app_rsgi.py @@ -1,4 +1,3 @@ -import asyncio import os from operator import itemgetter @@ -37,8 +36,6 @@ async def pg_setup(): with Path('templates/fortune.html').open('r') as f: template = jinja2.Template(f.read()) -asyncio.get_event_loop().run_until_complete(pg_setup()) - def get_num_queries(scope): try: @@ -152,6 +149,13 @@ async def handle_404(scope, proto): } -def main(scope, proto): - handler = routes.get(scope.path, handle_404) - return handler(scope, proto) +class App: + def __rsgi_init__(self, loop): + loop.run_until_complete(pg_setup()) + + def __rsgi__(self, scope, proto): + handler = routes.get(scope.path, handle_404) + return handler(scope, proto) + + +main = App() diff --git a/frameworks/Python/granian/granian-rsgi.dockerfile b/frameworks/Python/granian/granian-rsgi.dockerfile index 315493dc7ca..cba5380d5fd 100644 --- a/frameworks/Python/granian/granian-rsgi.dockerfile +++ b/frameworks/Python/granian/granian-rsgi.dockerfile @@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt EXPOSE 8080 -CMD python run.py rsgi runtime +CMD python run.py rsgi mt diff --git a/frameworks/Python/granian/granian-wrk.dockerfile b/frameworks/Python/granian/granian-wrk.dockerfile index 533a5bc722a..50a1e008ae8 100644 --- a/frameworks/Python/granian/granian-wrk.dockerfile +++ b/frameworks/Python/granian/granian-wrk.dockerfile @@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt EXPOSE 8080 -CMD python run.py rsgi workers +CMD python run.py rsgi st diff --git a/frameworks/Python/granian/granian-wsgi.dockerfile b/frameworks/Python/granian/granian-wsgi.dockerfile index 9ddcee211ff..1ecac416f49 100644 --- a/frameworks/Python/granian/granian-wsgi.dockerfile +++ b/frameworks/Python/granian/granian-wsgi.dockerfile @@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt EXPOSE 8080 -CMD python run.py wsgi runtime +CMD python run.py wsgi mt diff --git a/frameworks/Python/granian/granian.dockerfile b/frameworks/Python/granian/granian.dockerfile index 828808b1009..443c501448f 100644 --- a/frameworks/Python/granian/granian.dockerfile +++ b/frameworks/Python/granian/granian.dockerfile @@ -8,4 +8,4 @@ RUN pip install -r /granian/requirements.txt EXPOSE 8080 -CMD python run.py asgi runtime +CMD python run.py asgi mt diff --git a/frameworks/Python/granian/requirements.txt b/frameworks/Python/granian/requirements.txt index 1d8e4322ee2..1739b7a2386 100644 --- a/frameworks/Python/granian/requirements.txt +++ b/frameworks/Python/granian/requirements.txt @@ -1,4 +1,4 @@ asyncpg==0.29.0 -granian>=1.7.0,<1.8.0 +granian[uvloop]>=2.2.0,<2.3.0 jinja2==3.1.4 orjson==3.10.2 diff --git a/frameworks/Python/granian/run.py b/frameworks/Python/granian/run.py index 11bdee8d453..d24d571e4a3 100644 --- a/frameworks/Python/granian/run.py +++ b/frameworks/Python/granian/run.py @@ -6,7 +6,7 @@ if __name__ == '__main__': interface = sys.argv[1] - threading_mode = sys.argv[2] + runtime_mode = sys.argv[2] workers = multiprocessing.cpu_count() if interface == "rsgi": @@ -23,7 +23,7 @@ address="0.0.0.0", port=8080, workers=workers, - threading_mode=threading_mode, + runtime_mode=runtime_mode, blocking_threads=blocking_threads, backlog=16384, interface=interface,