Glossary
Multi-threaded scraping: threads, async and processes
Multi-threaded scraping is any design where several fetches or parse jobs run at once, and the choice between threads, async and processes shapes how far it scales.
Why a single loop is slow
A scraper that fetches one URL, parses it and then fetches the next spends most of its life idle, waiting for bytes. If a page takes 800 ms to arrive and 20 ms to parse, the CPU works about two percent of the time. Running several fetches together fills that idle time. The question is which mechanism you use to do it.
The three models
| Model | Good at | Watch out for |
|---|---|---|
| Threads | Blocking libraries such as Requests or Selenium; quick to add | Shared state needs locks; hundreds of threads cost memory |
| Async I/O | Thousands of open connections in one process | Every library in the path must be async; one blocking call stalls everything |
| Processes | CPU heavy parsing, image work, isolating browsers | Higher memory per worker; data passes through queues or files |
In Python, the global interpreter lock means threads do not speed up pure CPU work, but they do overlap network waits well. In Go, goroutines give you cheap concurrency by default. In Node.js, the event loop is async from the start, and worker threads exist for CPU heavy parts.
A thread pool in practice
from concurrent.futures import ThreadPoolExecutor, as_completed
import requests
pages = [f"https://example.com/catalog?p={n}" for n in range(1, 41)]
def grab(link):
with requests.Session() as s:
r = s.get(link, timeout=(5, 25))
return link, r.status_code, len(r.content)
with ThreadPoolExecutor(max_workers=8) as pool:
jobs = [pool.submit(grab, p) for p in pages]
for job in as_completed(jobs):
print(*job.result())
Each thread uses its own session here because sharing one Requests session across threads is not documented as safe. Eight workers is a starting point; tune it with the advice on concurrency.
Browsers in parallel
Browser automation is heavy. Each Chromium instance takes hundreds of megabytes, so ten browsers on a small server is often the limit. Prefer one browser with several isolated contexts (a Playwright feature) over many full browsers, and use processes or containers when you need hard isolation (see containerized scraping).
Challenges in a multi-worker setup
When several workers meet challenges at the same time, avoid making each worker block on its own polling loop. Two patterns work well. In the first, each worker creates its task and awaits it with an async sleep, which costs nothing in an event loop. In the second, workers hand the page to a dedicated solver component that creates tasks, polls them all in one loop and returns answers through a queue. Both fit GateSolver, since there is no limit on how many tasks you keep open, only on requests per minute. Image challenges finish quickly (a text image usually under 1 seconds), so poll those every second; token tasks every three.
Common bugs
- Writing to one CSV file from many threads without a lock, which interleaves rows.
- Reusing a solved token across workers; tokens are single use.
- Forgetting timeouts, so one hung connection holds a worker forever.
Common questions
Do Python threads make scraping faster?
Yes for network waits, because threads release the interpreter lock while waiting on sockets. They do not speed up CPU heavy parsing.
Should I choose async or threads for a new scraper?
Async if your HTTP client and other libraries support it and you need many connections; threads if you rely on blocking libraries.
How many browsers can I run in parallel?
It depends on memory. Each instance can take hundreds of megabytes, so prefer several contexts inside one browser where isolation allows.
Can workers share one solved CAPTCHA token?
No. A token is accepted once, so each request that needs one must use its own freshly solved token.
Is it safe to share one HTTP session between threads?
Some clients allow it and some do not. Giving each thread its own session is the safer default.
Keep reading
- ConcurrencyRunning many requests or solving tasks at the same time, and how per host limits and backpressure keep it under control.
- Containerized scrapingPackaging scrapers and their browsers into containers so the same worker runs anywhere and scales out by adding copies.
- Async scraping with aiohttpA bounded async crawler on aiohttp: one shared session, per host limits, jittered retries and CAPTCHA polling that never blocks.
- Python RequestsThe popular Python HTTP library with sessions and a simple API, and the reasons protected sites spot it so quickly.