feat: add sync subpackage for synchronous dependency resolution#3
Merged
Conversation
Code Coverage SummaryDetailsDiff against mainResults for commit: 2b7416a Minimum allowed coverage is ♻️ This comment has been updated with latest results |
Introduces fastapi_depends_anywhere.sync with sync equivalents of all core decorators, backed by a persistent background event loop thread. New public API: - sync.with_fastapi_depends — resolve deps, returns plain sync callable - sync.iter_with_fastapi_depends — resolve deps for sync/async generators - sync.with_fastapi_lifecycle — run within FastAPI lifespan synchronously - sync.runnify_with_fastapi_depends — lifecycle + deps + sync execution - sync.resolve_fastapi_depends — low-level sync context manager Implementation details: - Single daemon thread hosts a persistent asyncio event loop (functools.cache) - run_coroutine_threadsafe bridges sync callers to async dep resolution - No per-call event loop overhead; no extra dependencies required - Deadlock guard prevents calling _run_sync from within the loop thread - runnify_with_fastapi_depends runs lifecycle+deps in one coroutine to avoid nested run_coroutine_threadsafe deadlock Tests: 41 new tests with 100% coverage of the sync subpackage Docs: README updated with Synchronous Execution section and API reference
8614bd1 to
2b7416a
Compare
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.
Summary
Adds a
fastapi_depends_anywhere.syncsubpackage so FastAPI dependencies can be resolved from a synchronous context (Celery tasks, Django views, CLI scripts) without managing an event loop manually.New public API
sync.with_fastapi_dependssync.iter_with_fastapi_dependssync.with_fastapi_lifecyclesync.runnify_with_fastapi_dependssync.resolve_fastapi_dependsHow it works
A single daemon thread hosts a persistent
asyncioevent loop (lazily created viafunctools.cache). All sync-to-async bridging usesrun_coroutine_threadsafe, so there is no per-call event loop creation overhead and no additional dependencies are required.runnify_with_fastapi_dependsruns lifecycle + dependency resolution in a single coroutine to avoid the deadlock that would result from nesting two separaterun_coroutine_threadsafecalls.Changes
fastapi_depends_anywhere/sync/— new subpackage (5 files)tests/test_sync/— 41 new tests, 100% coverage of the sync subpackageREADME.md— new Synchronous Execution section and updated API referencefastapi_depends_anywhere/runners.py— removed now-redundanttype: ignore(asyncer stubs available)