{"id":26562606,"url":"https://github.com/newtypegeek/fastapi-async-trap","last_synced_at":"2026-05-19T14:06:16.756Z","repository":{"id":283316405,"uuid":"951330657","full_name":"newTypeGeek/fastapi-async-trap","owner":"newTypeGeek","description":"To understand how sync and async work in FastAPI","archived":false,"fork":false,"pushed_at":"2025-03-19T15:30:06.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-20T10:37:33.789Z","etag":null,"topics":["async","fastapi","sync"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/newTypeGeek.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-19T14:09:38.000Z","updated_at":"2025-03-19T15:30:10.000Z","dependencies_parsed_at":"2025-03-19T16:43:45.972Z","dependency_job_id":null,"html_url":"https://github.com/newTypeGeek/fastapi-async-trap","commit_stats":null,"previous_names":["newtypegeek/fastapi-async-trap"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/newTypeGeek/fastapi-async-trap","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newTypeGeek%2Ffastapi-async-trap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newTypeGeek%2Ffastapi-async-trap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newTypeGeek%2Ffastapi-async-trap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newTypeGeek%2Ffastapi-async-trap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/newTypeGeek","download_url":"https://codeload.github.com/newTypeGeek/fastapi-async-trap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/newTypeGeek%2Ffastapi-async-trap/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279001982,"owners_count":26083243,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","fastapi","sync"],"created_at":"2025-03-22T15:18:18.149Z","updated_at":"2025-10-09T19:11:10.610Z","avatar_url":"https://github.com/newTypeGeek.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastapi-async-trap\n\nThe document is generated by co-pilot with GPT-4o\n\n1. `/fake-async` Endpoint\n    - **Behavior**: This endpoint is marked as async, but it directly calls the synchronous function `services.heavy_computation_work()`. Since `heavy_computation_work()` is synchronous and uses `time.sleep`, it blocks the event loop while it runs.\n\n    - **Why It Blocks**: Even though the endpoint is `async`, the synchronous function (`heavy_computation_work`) runs in the main event loop, preventing other tasks from being processed until it completes.\n\n    - **Impact**: This endpoint will block other requests (both async and sync) because the event loop is occupied by the blocking synchronous function.\n\n2. `/real-async` Endpoint\n    - **Behavior**: This endpoint is marked as `async` and calls the asynchronous function `services.heavy_computation_work_async()` using `await`. The `heavy_computation_work_async()` function uses `await asyncio.sleep`, which is non-blocking.\n  \n    - **Why It Does Not Block**: the `await` keyword allows the event loop to continue processing other tasks while waiting for the `heavy_computation_work_async()` function to complete. This ensures that the event loop remains free to handle other requests.\n  \n    - **Impact**: This endpoint will not block other requests because it uses non-blocking asynchronous code.\n\n3. `/sync` Endpoint\n    - **Behavior**: This endpoint is synchronous and calls the synchronous function `services.heavy_computation_work()`. However, FastAPI automatically runs synchronous endpoints in a **thread pool executor**.\n  \n    - **Why It Does Not Block**: FastAPI offloads the execution of synchronous endpoints to a separate thread in the thread pool. This means the synchronous function runs in a background thread, leaving the main event loop free to handle other requests.\n\n    - **Impact**: This endpoint will not block other requests because it runs in a separate thread.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnewtypegeek%2Ffastapi-async-trap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnewtypegeek%2Ffastapi-async-trap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnewtypegeek%2Ffastapi-async-trap/lists"}