Title: Python Concurrency: Python asyncio — 47-Second Freeze
Open Graph Title: Python Concurrency: Python asyncio — 47-Second Freeze
Description: A sync requests.get() blocked the event loop for 47s—zero CPU, all coroutines frozen. Debug patterns you won't find in GFG's basic tutorials.
Open Graph Description: A sync requests.get() blocked the event loop for 47s—zero CPU, all coroutines frozen. Debug patterns you won't find in GFG's basic tutorials.
Opengraph URL: https://thecodeforge.io/python/python-asyncio-deep-dive/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/python-asyncio-deep-dive/"
},
"headline": "Python asyncio — 47-Second Freeze from Sync Calls",
"description": "A sync requests.get() blocked the event loop for 47s—zero CPU, all coroutines frozen. Debug patterns you won't find in GFG's basic tutorials.",
"keywords": "Python asyncio — 47-Second Freeze from Sync Calls, Python, Advanced, Advanced Python, A sync requests.get() blocked the event loop for 47s—zero CPU, all coroutines frozen. Debug patterns you won't find in GFG's basic tutorials.",
"proficiencyLevel": "Expert",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-06T00:00:00+00:00",
"dateModified": "2026-09-26T00:00:00+00:00",
"url": "https://thecodeforge.io/python/python-asyncio-deep-dive/",
"inLanguage": "en",
"educationalLevel": "Advanced",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Python asyncio — 47-Second Freeze from Sync Calls", "item": "https://thecodeforge.io/python/python-asyncio-deep-dive/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the difference between asyncio and threading in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Threading relies on the OS to schedule and switch between threads. Because of Python's GIL, only one thread executes Python bytecode at a time regardless of how many threads exist — so threading does not provide CPU parallelism for Python code, though it does release the GIL during I/O operations and C extension calls.\n\nasyncio uses a single OS thread with cooperative multitasking. Context switches happen only when a coroutine explicitly yields with await, which means zero OS overhead per switch. The memory footprint is dramatically lower — a coroutine is a few kilobytes; an OS thread stack is typically one to eight megabytes. For high-concurrency I/O workloads, asyncio scales to tens of thousands of concurrent operations where threading would exhaust memory and OS thread limits at a few hundred.\n\nUse asyncio for new high-concurrency I/O code. Use threading when integrating with legacy synchronous libraries via run_in_executor() or when a specific library explicitly requires a dedicated thread."
}
},
{
"@type": "Question",
"name": "When should I use asyncio.create_task() instead of gather()?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use asyncio.create_task() when you want to start a background operation now and collect its result at a later point in the same code path, or when you want fire-and-forget behaviour where you do not need the result at all. The task starts immediately when create_task() is called and runs concurrently with whatever the current coroutine does next.\n\nUse asyncio.gather() when you have a known list of independent operations and you need all of their results before you can proceed. gather() is the right tool for fanout patterns: fetch from all these sources, wait for all results, then process them together.\n\nThe practical distinction: create_task() gives you fine-grained control over individual task lifetimes. gather() is a convenient all-or-nothing collection mechanism. For structured concurrency in Python 3.11+, TaskGroup combines the control of create_task() with the wait-for-all semantics of gather() and adds automatic sibling cancellation on failure."
}
},
{
"@type": "Question",
"name": "Can I use asyncio for CPU-intensive tasks like image processing or ML inference?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No, not directly. asyncio is single-threaded — a CPU-intensive operation running inside the event loop will block the loop for its entire duration, freezing all other coroutines. CPU work does not release the event loop the way I/O does.\n\nFor CPU-bound work, use multiprocessing to bypass the GIL and utilise multiple cores. If you need to integrate CPU-bound work into an async application, use loop.run_in_executor(ProcessPoolExecutor, cpu_function, data) — this offloads the CPU work to a separate process and returns an awaitable so the event loop remains free.\n\nThe overhead of IPC serialisation makes ProcessPoolExecutor most appropriate for coarse-grained work — process this entire batch — rather than fine-grained per-request work where the IPC cost exceeds the computation time."
}
},
{
"@type": "Question",
"name": "How do I test async code with pytest?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Use the pytest-asyncio plugin. Install it with pip install pytest-asyncio, then mark test functions with @pytest.mark.asyncio and define them as async def. The plugin manages the event loop lifecycle for each test.\n\nFor async fixtures, use @pytest_asyncio.fixture with async def and await inside them normally. Configure the plugin mode in pytest.ini or pyproject.toml: asyncio_mode = auto applies the asyncio mark automatically to all async test functions, removing boilerplate.\n\nFor testing timeout and cancellation behaviour specifically, asyncio.wait_for() in combination with asyncio.sleep() inside test fixtures allows you to simulate slow dependencies. Mock async functions with AsyncMock from unittest.mock rather than MagicMock — AsyncMock returns an awaitable, MagicMock does not and will cause errors when the code under test awaits it."
}
},
{
"@type": "Question",
"name": "What happens to pending tasks when the event loop closes?",
"acceptedAnswer": {
"@type": "Answer",
"text": "When asyncio.run() returns, it cancels all tasks that are still pending and runs the loop briefly to allow them to handle CancelledError. Tasks that have try/finally or async context manager cleanup will execute their cleanup code. Tasks that do not handle CancelledError will have any code after their current await point skipped.\n\nFor graceful shutdown where you need all tasks to complete cleanly, implement explicit shutdown logic before returning from main(): collect all tasks with asyncio.all_tasks(), cancel each one, and then await asyncio.gather(*pending_tasks, return_exceptions=True) to let each task run its cleanup. After this, the loop closes with no orphaned tasks.\n\nFor long-running services, wire SIGTERM handling to this shutdown sequence so that Kubernetes pod termination or systemd stop triggers a clean drain rather than an abrupt kill."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large