Skip to content

Instantly share code, notes, and snippets.

@daviskirk
Last active April 29, 2021 20:52
Show Gist options
  • Save daviskirk/7e8495ca5b8150f9002c5bc80630fa5a to your computer and use it in GitHub Desktop.
Save daviskirk/7e8495ca5b8150f9002c5bc80630fa5a to your computer and use it in GitHub Desktop.
bug report for coverage using starlette and async/await sqlalchemy
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"
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%
sqlalchemy~=1.4
asyncpg
requests
starlette
coverage
gevent
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
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()
@daviskirk
Copy link
Author

daviskirk commented Dec 28, 2020

Updated with greenlet, gevent and thread concurrencies:

Default (threading):

image

Greenlet and Gevent:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment