Browse Source

[Python] Bump Granian to 2.2 (#9811)

Giovanni Barillari 3 months ago
parent
commit
fb78929b96

+ 23 - 5
frameworks/Python/granian/app_asgi.py

@@ -55,8 +55,6 @@ json_dumps = orjson.dumps
 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 @@ routes = {
 }
 
 
-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()

+ 10 - 6
frameworks/Python/granian/app_rsgi.py

@@ -1,4 +1,3 @@
-import asyncio
 import os
 
 from operator import itemgetter
@@ -37,8 +36,6 @@ json_dumps = orjson.dumps
 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 @@ routes = {
 }
 
 
-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()

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 1 - 1
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

+ 2 - 2
frameworks/Python/granian/run.py

@@ -6,7 +6,7 @@ from granian import Granian
 
 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 @@ if __name__ == '__main__':
         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,