Browse Source

Upgrade aiohttp (#9348)

* Upgrade aiohttp

* Update aiohttp.dockerfile

* Update aiohttp-pg-raw.dockerfile

* Update aiohttp-pg-raw.dockerfile

* Update main.py

* Update models.py

* Update frameworks/Python/aiohttp/requirements.txt

* Update models.py

* Update aiohttp.dockerfile

* Update aiohttp-pg-raw.dockerfile

* Update requirements.txt

* Update requirements.txt

* Update requirements.txt

* Update aiohttp-pg-raw.dockerfile

* Update aiohttp.dockerfile

* Update main.py

* Update requirements.txt

* Update aiohttp-pg-raw.dockerfile

* Update aiohttp.dockerfile
Sam Bull 4 months ago
parent
commit
feca8319c5

+ 3 - 3
frameworks/Python/aiohttp/aiohttp-pg-raw.dockerfile

@@ -1,14 +1,14 @@
-FROM python:3.8
+FROM python:3.13
 
 ADD ./ /aiohttp
 
 WORKDIR aiohttp
 
-RUN pip3 install cython==0.29.23 && \
+RUN pip3 install cython==3.0.11 && \
     pip3 install -r /aiohttp/requirements.txt
 
 ENV CONNECTION=RAW
 
 EXPOSE 8080
 
-CMD gunicorn app.gunicorn:app -c gunicorn_conf.py
+CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py

+ 3 - 3
frameworks/Python/aiohttp/aiohttp.dockerfile

@@ -1,14 +1,14 @@
-FROM python:3.8
+FROM python:3.13
 
 ADD ./ /aiohttp
 
 WORKDIR aiohttp
 
-RUN pip3 install cython==0.29.23 && \
+RUN pip3 install cython==3.0.11 && \
     pip3 install -r /aiohttp/requirements.txt
 
 WORKDIR /aiohttp
 
 EXPOSE 8080
 
-CMD gunicorn app.gunicorn:app -c gunicorn_conf.py
+CMD python3 -O -m gunicorn app.gunicorn:app -c gunicorn_conf.py

+ 9 - 7
frameworks/Python/aiohttp/app/main.py

@@ -4,8 +4,7 @@ import multiprocessing
 import asyncpg
 from aiohttp import web
 from sqlalchemy.engine.url import URL
-from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
-from sqlalchemy.orm import sessionmaker
+from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
 
 from .views import (
     json,
@@ -28,14 +27,15 @@ def pg_dsn(dialect=None) -> str:
     """
     :return: DSN url suitable for sqlalchemy and aiopg.
     """
-    return str(URL.create(
+    url = URL.create(
         database='hello_world',
         password=os.getenv('PGPASS', 'benchmarkdbpass'),
         host='tfb-database',
         port='5432',
         username=os.getenv('PGUSER', 'benchmarkdbuser'),
         drivername='postgresql+{}'.format(dialect) if dialect else 'postgresql',
-    ))
+    )
+    return url.render_as_string(hide_password=False)
 
 
 async def db_ctx(app: web.Application):
@@ -48,15 +48,17 @@ async def db_ctx(app: web.Application):
     print(f'connection pool: min size: {min_size}, max size: {max_size}, orm: {CONNECTION_ORM}')
     if CONNECTION_ORM:
         dsn = pg_dsn('asyncpg')
-        engine = create_async_engine(dsn, future=True, pool_size=max_size)
-        app['db_session'] = sessionmaker(engine, class_=AsyncSession)
+        engine = create_async_engine(dsn, pool_size=max_size)
+        app['db_session'] = async_sessionmaker(engine)
     else:
         dsn = pg_dsn()
         app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop)
 
     yield
 
-    if not CONNECTION_ORM:
+    if CONNECTION_ORM:
+        await app['db_session'].dispose()
+    else:
         await app['pg'].close()
 
 

+ 7 - 7
frameworks/Python/aiohttp/app/models.py

@@ -1,20 +1,20 @@
-from sqlalchemy import Column, Integer, String
-from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
 
-Base = declarative_base()
+class Base(DeclarativeBase):
+    """Base for models."""
 
 
 class World(Base):
     __tablename__ = 'world'
-    id = Column(Integer, primary_key=True)
-    randomnumber = Column(Integer)
+    id: Mapped[int] = mapped_column(primary_key=True)
+    randomnumber: Mapped[int]
 
 sa_worlds = World.__table__
 
 
 class Fortune(Base):
     __tablename__ = 'fortune'
-    id = Column(Integer, primary_key=True)
-    message = Column(String)
+    id: Mapped[int] = mapped_column(primary_key=True)
+    message: Mapped[str]
 
 sa_fortunes = Fortune.__table__

+ 8 - 9
frameworks/Python/aiohttp/requirements.txt

@@ -1,9 +1,8 @@
-aiohttp==3.10.2
-asyncpg==0.25.0
-cchardet==2.1.7
-gunicorn==20.1
-jinja2==3.1.4
-psycopg2==2.9.2
-SQLAlchemy==1.4.29
-ujson==5.4.0
-uvloop==0.16
+aiohttp==3.11.14
+asyncpg==0.30.0
+gunicorn==23.0.0
+jinja2==3.1.5
+psycopg2==2.9.10
+SQLAlchemy==2.0.39
+ujson==5.10.0
+uvloop==0.21.0