Member-only story
Making Python Faster — ThreadPoolExecutor vs AsyncIO vs Multiprocessing/ProcessPoolExecutor
Sometimes, running code sequentially is terrible
Imagine you’re building a script that fetches data from 1000 APIs, does some processing, then writes results somewhere.
If we run it sequentially eg. for url in urls: fetch(url), our program (one process, one thread) spends most of its time waiting for 1) network responses 2) files to write.
This becomes a huge problem as our app grows in complexity/scale — imagine doing this for 10,000,000 APIs instead. As such, as complexity/scale increases, sequential execution becomes a bottleneck.
Python gives us 3 powerful tools to break out of this 1) asyncio 2) thread pool executor 3) multiprocessing — but picking the wrong one can make things slower instead of faster.
Concurrency VS Parallelism
Concurrency means dealing with multiple tasks at once — our program juggles (context-switches) between tasks without necessarily running them at the same time. This is usually done using 1 CPU core.
Parallelism means actually doing multiple tasks at once using multiple CPU cores.
