SShortSingh.
Back to feed

How adding 'async def' in FastAPI can silently freeze your entire service

0
·1 views

A common FastAPI refactor — converting plain def endpoints to async def for cleaner code — can trigger a severe performance incident where every endpoint, including lightweight health checks, slows simultaneously. The root cause lies in how FastAPI handles the two endpoint types: plain def functions are offloaded to a 40-thread worker pool, while async def functions run directly on the single-threaded event loop. When a developer adds async def without replacing blocking calls inside — such as synchronous HTTP requests or blocking database queries — those calls monopolize the event loop, stalling all other requests for the duration. Unlike Node.js, where the event loop is the danger zone, Python's FastAPI uses the threadpool as a safety net, meaning removing a function from that pool by marking it async is a downgrade, not an upgrade. The correct approach is to either keep blocking code in plain def endpoints or fully replace blocking libraries with async-compatible alternatives like httpx before switching to async def.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Developer builds live registry tracking 750+ MCP servers across GitHub

A developer has launched kiprio.com/mcp-registry, a daily-updated registry that indexes, categorises, and ranks over 750 Model Context Protocol (MCP) servers sourced from GitHub and community lists. MCP, which allows AI assistants like Claude to connect to databases, APIs, and file systems, has grown rapidly over the past six months with no centralised discovery tool available. The registry reveals that Python and TypeScript each account for roughly a third of all servers, with Python favoured for data-heavy integrations and TypeScript leading in production-quality implementations. Analysis of the indexed servers shows significant redundancy — with over 40 weather servers and 30-plus web search wrappers — alongside genuinely useful tools for database querying, GitHub workflows, and niche use cases like Ethereum data and betting markets. A free public API is available for searching and retrieving server data, with a paid Pro tier offering bulk access and webhook notifications.

0
ProgrammingDEV Community ·

How to manage local and staging domains cleanly using hosts file profiles

Developers frequently end up with cluttered /etc/hosts files containing conflicting entries for the same hostname across local, staging, and cutover environments. A practical solution involves maintaining separate hosts profile files in a dedicated directory and activating only one at a time by copying it over the system hosts file. A simple shell script can automate this process, backing up the existing hosts file with a timestamp and flushing the DNS cache after each switch. For setups requiring shared base entries alongside project-specific overrides, profiles can be concatenated using cat and tee before applying. This approach is recommended until more complex tooling like dnsmasq or CoreDNS becomes necessary for wildcard domains or team-wide dynamic DNS needs.

0
ProgrammingDEV Community ·

RegionCheck Launches Tool to Test Endpoints Across AWS, Azure, and Google Cloud

A developer has launched RegionCheck, a new platform that allows engineers to test and monitor endpoints from cloud regions across AWS, Azure, and Google Cloud without setting up any infrastructure. The tool supports HTTP testing, DNS lookups, TLS certificate validation, continuous monitoring with alerts, and side-by-side comparisons across providers. It was built to address the lack of a single unified tool for diagnosing issues like uneven DNS propagation, CDN caching problems, and regional latency differences. RegionCheck is aimed at DevOps engineers, SREs, platform engineers, and backend developers who work with cloud infrastructure. The platform is available at regioncheck.io, with free checks open to all visitors and additional features accessible via a registered account.

0
ProgrammingDEV Community ·

A Safer Workflow for Editing /etc/hosts Without Breaking Local Dev Setups

Editing /etc/hosts is a common practice for local development, but mistakes like duplicate entries or skipped DNS flushes can cause hard-to-debug issues. A recommended approach involves maintaining a personal copy of the hosts file, backing up the live system file before overwriting it, and applying changes deliberately rather than editing the system file directly. After copying the updated file to the system path, developers should flush DNS using the appropriate command for their OS — macOS, Windows, or Linux — and verify resolution via terminal before opening a browser. Keeping only one active mapping per hostname and confirming changes in the terminal first helps separate hosts-related bugs from application-level issues. For teams, committing a hosts.example file and a simple apply script to the repository can prevent configuration drift and speed up onboarding.

How adding 'async def' in FastAPI can silently freeze your entire service · ShortSingh