Repository navigation
Conversation
added 3 commits
September 18, 2026 10:14
RestEndpoint carried every CRUD method twice: list, retrieve, create, update and destroy, and again as _list_async ... _destroy_async, copied line by line with `await` added. update alone was 107 lines, twice. Any fix had to be made in both copies. The logic now lives once in _list, _retrieve, _create, _update and _destroy, which take an open sync Session. The public methods are thin: - list/retrieve/create/update/destroy open a sync session (get_sync_session) and call the core. - the _*_async helpers call the same core through AsyncSession.run_sync, which is SQLAlchemy's supported way to run sync ORM code on an async driver. Names and signatures do not change. The _*_async helpers and paginate_async stay because the README, docs/advanced/async.md and three examples tell users to call them. paginate_async now delegates to paginate the same way; it had no test, so it gets one. Checked on aiosqlite: the five test_async_* files and test_background_tasks pass through the new path (insert with refresh, update with rowcount, delete with RETURNING, both paginators, async queryset, reflection). Not checked on asyncpg: no Postgres here. Tests: 329 passed (327 + 2 new), 1 xfailed, 2 xpassed.
With a sync Engine the route handlers are `async def` but called endpoint.list(), create(), update() and the rest directly, so every database round trip ran on the event loop thread. One slow query stalled all other requests on that worker. The Redis cache calls had the same problem. The sync branches now go through session.run_blocking, which uses Starlette's run_in_threadpool. Cache invalidation goes the same way and is skipped for GET, which never invalidates. Engines on StaticPool or SingletonThreadPool keep running in place. Those are the in-memory SQLite pools: SingletonThreadPool gives every thread its own empty database, and StaticPool shares one connection that is not safe across threads. The docs install snippet and most examples use them, and they would have broken under a plain thread pool. tests/test_sync_offload.py pins both cases by comparing the thread that runs the queryset with the event loop thread. It fails without this change. Tests: 331 passed, 1 xfailed, 2 xpassed.
maybe_cached and the cache invalidation lived only in the sync branches of the route handlers. An app on an AsyncEngine with `cache = Cache(...)` never read, wrote or invalidated anything, with no warning. cache_helper now splits the work into _cached_response and _store_response. maybe_cached (sync) and the new maybe_cached_async share them, so the rules exist once. The Redis client is blocking, so the async path sends those calls to a worker thread and skips the hop when the endpoint has no cache configured. invalidate_cache_after_write replaces maybe_invalidate_cache and is called for both engine kinds, which removes one `if not is_async` from each handler. tests/test_cache_async.py covers hit, miss with the stored key, body and ttl, invalidation on POST, and no invalidation on GET. Three of the four fail without this change. Tests: 335 passed, 1 xfailed, 2 xpassed.
This was referenced Sep 18, 2026
Closed
Closed
Owner
Author
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #38. Merge that one first; GitHub will then point this PR at
dev.What
Three commits:
RestEndpointhad every CRUD method twice:list,retrieve,create,update,destroy, and again as_list_async..._destroy_async, copied line by line withawaitadded.updatealone was 107 lines, twice. The logic now lives once in_list,_retrieve,_create,_updateand_destroy, which take an open syncSession. The sync methods open a session and call them. The_*_asynchelpers call the same code throughAsyncSession.run_sync.paginate_asyncdoes the same withpaginate.rest.pygoes from 807 to 613 lines.async defbut calledendpoint.list(),create()and the rest directly, so with a syncEngineone slow query stalled every other request on that worker. They now go through Starlette's thread pool.Meta.cacheworks on async engines. Cached GETs and cache invalidation only existed on the sync path. An app on anAsyncEnginewithcache = Cache(...)never read or wrote the cache, with no warning.What does not change
Names and signatures. The
_*_asynchelpers andpaginate_asyncstay, because the README,docs/advanced/async.md,docs/api-reference/pagination.mdand three examples tell users to call them.Behaviour changes to know about
StaticPoolorSingletonThreadPoolstill run in place. Those are the in-memory SQLite pools:SingletonThreadPoolgives each thread its own empty database, andStaticPoolshares one connection that is not safe across threads. The install snippet in the docs and most examples use them, and a plain thread pool would have broken them.Meta.cachestart using Redis. Before this they silently did not.Both are in
CHANGELOG.mdunder a new[Unreleased]section. I left the old "Unreleased 0.1.24" section as it was.Checks
All run on my machine. The test workflow does not run for PRs into
dev.pytest: 335 passed, 1 xfailed, 2 xpassed. That is the 327 from before plus 8 new ones.paginate_async(2), thread used for sync CRUD on a pooled engine and onStaticPool(2), async cache hit, miss, invalidation on POST, no invalidation on GET (4).test_async_*files andtest_background_tasks(34 tests) pass through the new single path on aiosqlite: insert with refresh, update with rowcount, delete with RETURNING, both paginators, async queryset, reflection.lightapi/: 82% before, 84% now (2,251 statements, 357 missed).ruff check,ruff format --check,mypy: clean.Not checked: Postgres with asyncpg. I have no Postgres here, so
run_syncis only proven on aiosqlite.