CPU-bound vs I/O-bound — multiprocessing and asyncio

Beginner Data Science Praktikum
Created by Pavel · 21.03.2026 at 01:05 UTC · 1 completed

Picture two workloads. In the first, you fire two hundred small HTTP requests and most of the runtime is thumb-twiddling while packets return—your CPU is bored. In the second, you grind through millions of Python loop iterations with no I/O—the cores are busy, and adding more waiting does not help.

Concurrency is about where time goes. For heavy pure-Python CPU work, the GIL keeps one interpreter from using threads as true parallel compute, so multiprocessing (separate processes) is the usual path to multiple cores. For I/O-bound storms of network or disk waits, asyncio with an async HTTP stack like aiohttp overlaps many waits in one thread instead of spawning a process per URL.

The cautionary beat: parallelizing tiny tasks can cost more in scheduling than you gain, and dropping a blocking call into an asyncio loop stalls the whole orchestra unless you offload with run_in_executor.

Contrast this with synchronous requests-style scripts in requests, JSON APIs, and robust fetching, where calls block until each response returns. Intro: [1].


Sources

University approvals: 0
Tasks
Question 1

You need to fetch 200 small JSON documents from public APIs over the network. The total runtime is dominated by waiting for responses. Which approach is usually the best first choice?

Hint

Identify whether the bottleneck is CPU or waiting.

Question 2

You run a tight pure-Python loop over millions of rows with no external calls. Why might multiprocessing help more than asyncio here?

Hint

Recall what asyncio is designed to multiplex.

Question 3

Implement concurrency_pick(kind: str) -> str with normalized mapping rules:
- Treat cpu, cpu-bound, and compute as CPU-bound and return multiprocessing
- Treat io, i/o, network, and disk as I/O-bound and return asyncio
- Return unknown otherwise

Normalization: trim spaces and lowercase before matching. Submit the function; expression mode tests call it.

Hint

Normalize with strip().lower() before branching.

Starter code is prefilled; replace TODO blocks with your solution.
3 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Data Science Praktikum
  • Difficulty: Beginner
  • Completed: 1 users
Creator
Pavel
Pavel