Browse Source

[aiohttp] Updates (#6247)

* Update aiohttp test.

* Update main.py

Co-authored-by: Sam Bull <[email protected]>
Sam Bull 4 years ago
parent
commit
66a530a444

+ 1 - 4
frameworks/Python/aiohttp/app/gunicorn.py

@@ -1,6 +1,3 @@
-import asyncio
-
 from .main import create_app
 
-loop = asyncio.get_event_loop()
-app = create_app(loop)
+app = create_app()

+ 5 - 6
frameworks/Python/aiohttp/app/main.py

@@ -42,7 +42,7 @@ def pg_dsn() -> str:
     ))
 
 
-async def startup(app: web.Application):
+async def db_ctx(app: web.Application):
     dsn = pg_dsn()
     # number of gunicorn workers = multiprocessing.cpu_count() as per gunicorn_conf.py
     # max_connections = 2000 as per toolset/setup/linux/databases/postgresql/postgresql.conf:64
@@ -56,8 +56,8 @@ async def startup(app: web.Application):
     else:
         app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop)
 
+    yield
 
-async def cleanup(app: web.Application):
     if CONNECTION_ORM:
         app['pg'].close()
         await app['pg'].wait_closed()
@@ -80,14 +80,13 @@ def setup_routes(app):
         app.router.add_get('/updates/{queries:.*}', updates_raw)
 
 
-def create_app(loop):
-    app = web.Application(loop=loop)
+def create_app():
+    app = web.Application()
 
     jinja2_loader = jinja2.FileSystemLoader(str(THIS_DIR / 'templates'))
     aiohttp_jinja2.setup(app, loader=jinja2_loader)
 
-    app.on_startup.append(startup)
-    app.on_cleanup.append(cleanup)
+    app.cleanup_ctx.append(db_ctx)
 
     setup_routes(app)
     return app

+ 3 - 5
frameworks/Python/aiohttp/app/views.py

@@ -1,18 +1,16 @@
+from functools import partial
 from operator import attrgetter, itemgetter
 from random import randint
 
 from aiohttp_jinja2 import template
-from aiohttp.web import Response
+from aiohttp.web import Response, json_response
 import ujson
 
 from sqlalchemy import select
 
 from .models import sa_fortunes, sa_worlds, Fortune
 
-
-def json_response(data):
-    body = ujson.dumps(data)
-    return Response(body=body.encode(), content_type='application/json')
+json_response = partial(json_response, dumps=ujson.dumps)
 
 
 def get_num_queries(request):

+ 5 - 5
frameworks/Python/aiohttp/requirements.txt

@@ -1,10 +1,10 @@
-aiohttp==3.6.2
-aiohttp-jinja2==1.2.0
+aiohttp==3.7.3
+aiohttp-jinja2==1.4.2
 aiopg==1.0.0
-asyncpg==0.20.1
-cchardet==2.1.6
+asyncpg==0.21.0
+cchardet==2.1.7
 gunicorn==20.0.4
-psycopg2==2.8.5
+psycopg2==2.8.6
 SQLAlchemy==1.3.16
 ujson==2.0.3
 uvloop==0.14.0