Last active
May 21, 2024 02:58
-
-
Save daviskirk/7e8495ca5b8150f9002c5bc80630fa5a to your computer and use it in GitHub Desktop.
bug report for coverage using starlette and async/await sqlalchemy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
services: | |
postgres: | |
restart: always | |
image: postgres:12-alpine | |
environment: | |
POSTGRES_USER: "postgres" | |
POSTGRES_PASSWORD: "postgres" | |
ports: | |
- "127.0.0.1:5432:5432" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
runing with default concurrency | |
start | |
{'?column?': 1} | |
start | |
{'?column?': 1} | |
Name Stmts Miss Cover | |
---------------------------------------------------------------- | |
starlette_async_sqlalchemy_coverage_bug.py 29 9 69% | |
---------------------------------------------------------------- | |
TOTAL 29 9 69% | |
running with greenlet | |
start | |
{'?column?': 1} | |
start | |
{'?column?': 1} | |
Name Stmts Miss Cover | |
---------------------------------------------------------------- | |
starlette_async_sqlalchemy_coverage_bug.py 29 6 79% | |
---------------------------------------------------------------- | |
TOTAL 29 6 79% | |
running with gevent | |
start | |
{'?column?': 1} | |
start | |
{'?column?': 1} | |
Name Stmts Miss Cover | |
---------------------------------------------------------------- | |
starlette_async_sqlalchemy_coverage_bug.py 29 6 79% | |
---------------------------------------------------------------- | |
TOTAL 29 6 79% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sqlalchemy~=1.4 | |
asyncpg | |
requests | |
starlette | |
coverage | |
gevent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker-compose up -d | |
echo "runing with default concurrency" | |
coverage run starlette_async_sqlalchemy_coverage_bug.py | |
coverage report | |
coverage html | |
echo "running with greenlet" | |
coverage run --concurrency greenlet starlette_async_sqlalchemy_coverage_bug.py | |
coverage report | |
coverage html -d htmlcov_greenlet | |
echo "running with gevent" | |
coverage run --concurrency greenlet starlette_async_sqlalchemy_coverage_bug.py | |
coverage report | |
coverage html -d htmlcov_greenlet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy as sa | |
from sqlalchemy.ext.asyncio import create_async_engine | |
from starlette.applications import Starlette | |
from starlette.testclient import TestClient | |
from starlette.routing import Route | |
from starlette.responses import JSONResponse | |
engine = create_async_engine('postgresql+asyncpg://postgres:postgres@localhost/postgres') | |
engine2 = sa.create_engine('postgresql+asyncpg://postgres:postgres@localhost/postgres') | |
async def run(*args, **kwargs): | |
print('start') | |
async with engine.begin() as conn: | |
result = await conn.execute(sa.text("select 1")) | |
records = dict(result.mappings().first()) | |
print(records) | |
return JSONResponse(records) | |
def run2(*args, **kwargs): | |
print('start') | |
with engine2.begin() as conn: | |
result = conn.execute(sa.text("select 1")) | |
records = dict(result.mappings().first()) | |
print(records) | |
return JSONResponse(records) | |
app = Starlette(debug=True, routes=[ | |
Route('/run', run), | |
Route('/run2', run), | |
]) | |
def test_run(): | |
client = TestClient(app) | |
client.get('/run') | |
client.get('/run2') | |
if __name__ == "__main__": | |
test_run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated with greenlet, gevent and thread concurrencies:
Default (threading):
Greenlet and Gevent: