Skip to content

Commit 66a530a

Browse files
[aiohttp] Updates (#6247)
* Update aiohttp test. * Update main.py Co-authored-by: Sam Bull <[email protected]>
1 parent 9cfd171 commit 66a530a

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import asyncio
2-
31
from .main import create_app
42

5-
loop = asyncio.get_event_loop()
6-
app = create_app(loop)
3+
app = create_app()

frameworks/Python/aiohttp/app/main.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def pg_dsn() -> str:
4242
))
4343

4444

45-
async def startup(app: web.Application):
45+
async def db_ctx(app: web.Application):
4646
dsn = pg_dsn()
4747
# number of gunicorn workers = multiprocessing.cpu_count() as per gunicorn_conf.py
4848
# max_connections = 2000 as per toolset/setup/linux/databases/postgresql/postgresql.conf:64
@@ -56,8 +56,8 @@ async def startup(app: web.Application):
5656
else:
5757
app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop)
5858

59+
yield
5960

60-
async def cleanup(app: web.Application):
6161
if CONNECTION_ORM:
6262
app['pg'].close()
6363
await app['pg'].wait_closed()
@@ -80,14 +80,13 @@ def setup_routes(app):
8080
app.router.add_get('/updates/{queries:.*}', updates_raw)
8181

8282

83-
def create_app(loop):
84-
app = web.Application(loop=loop)
83+
def create_app():
84+
app = web.Application()
8585

8686
jinja2_loader = jinja2.FileSystemLoader(str(THIS_DIR / 'templates'))
8787
aiohttp_jinja2.setup(app, loader=jinja2_loader)
8888

89-
app.on_startup.append(startup)
90-
app.on_cleanup.append(cleanup)
89+
app.cleanup_ctx.append(db_ctx)
9190

9291
setup_routes(app)
9392
return app

frameworks/Python/aiohttp/app/views.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
from functools import partial
12
from operator import attrgetter, itemgetter
23
from random import randint
34

45
from aiohttp_jinja2 import template
5-
from aiohttp.web import Response
6+
from aiohttp.web import Response, json_response
67
import ujson
78

89
from sqlalchemy import select
910

1011
from .models import sa_fortunes, sa_worlds, Fortune
1112

12-
13-
def json_response(data):
14-
body = ujson.dumps(data)
15-
return Response(body=body.encode(), content_type='application/json')
13+
json_response = partial(json_response, dumps=ujson.dumps)
1614

1715

1816
def get_num_queries(request):
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
aiohttp==3.6.2
2-
aiohttp-jinja2==1.2.0
1+
aiohttp==3.7.3
2+
aiohttp-jinja2==1.4.2
33
aiopg==1.0.0
4-
asyncpg==0.20.1
5-
cchardet==2.1.6
4+
asyncpg==0.21.0
5+
cchardet==2.1.7
66
gunicorn==20.0.4
7-
psycopg2==2.8.5
7+
psycopg2==2.8.6
88
SQLAlchemy==1.3.16
99
ujson==2.0.3
1010
uvloop==0.14.0

0 commit comments

Comments
 (0)