All articles4 min read
Collection engineering

Bound scraper concurrency without building an endless queue

Limit active work and pending submissions separately, then measure accepted records.

Illustrative editorial image of a developer managing active collection jobs beside pending and completed cards.

Setting a worker limit is a useful start, but it does not bound every part of a collection job. A crawler can run only a few requests at once while holding an enormous backlog of submitted tasks. Separate active work, pending work and completed output before increasing the size of a run.

Give each limit a specific job

Active-request concurrency controls how many requests are in flight. A submission limit controls how much work has been handed to an executor or queue. A request-rate policy controls starts over time. Those settings answer different questions; a small worker pool can still produce bursts when responses arrive quickly.

Choose limits for the target and workload you are authorized to collect. Apply separate budgets when different hosts have different tolerances or access conditions. Proxy allocation does not remove the target's limits, and changing routes should not be treated as permission to increase load.

Avoid submitting the whole input immediately

A thread pool limits the number of workers executing calls, but repeatedly submitting every item can still create a large collection of pending futures and associated input data. Track the work waiting to start, rather than assuming the worker count covers it.

A bounded producer-consumer queue is one design. With a positive maximum size, a blocking put waits when the queue is full. An unbounded queue offers no such backpressure. Make sure the queue you choose actually has a capacity, and define how workers acknowledge completed or failed items.

For a small collector, processing finite batches can be simpler to inspect. It leaves some throughput unused while a slow item finishes, but makes the submission boundary explicit. That tradeoff is often useful before moving to a continuously replenished scheduler.

Start with a visible submission boundary

This scheduling example submits at most sixteen items from the input iterator per batch and executes at most four worker calls concurrently. The numbers are illustrative, not recommended target rates.

from concurrent.futures import ThreadPoolExecutor, as_completed
from itertools import islice

def collect_in_batches(items, fetch, workers=4, batch_size=16):
    if workers < 1 or batch_size < 1:
        raise ValueError("limits must be positive")
    iterator = iter(items)
    with ThreadPoolExecutor(max_workers=workers) as pool:
        while True:
            batch = list(islice(iterator, batch_size))
            if not batch:
                return
            futures = [pool.submit(fetch, item) for item in batch]
            for future in as_completed(futures):
                yield future.result()

Supply your own fetch function with appropriate network timeouts, response-size limits and destination controls. This helper does not implement retries, per-host rate limits or durable checkpoints. Results are yielded in completion order, so keep the input identifier in each result if downstream processing needs to match observations to requests.

Make failure behavior deliberate

If a worker raises an exception, retrieving its result raises that exception too. The helper above therefore stops yielding on the first observed failure. The executor's context manager waits for submitted work during shutdown; stopping iteration does not imply that all network activity has immediately stopped.

Define how production workers represent failures and how the scheduler drains outstanding work. Cancelling a pending future does not stop a call that is already running. Put timeouts in the network operation itself and keep retry attempts inside the same overall work budget.

Do not resubmit failed items indefinitely into a separate queue that has no capacity or attempt cap. A temporary failure can otherwise increase the backlog faster than successful requests reduce it. Keep retry eligibility and scheduling separate from the accepted-record count.

Tune against useful output

Measure pending items, active calls, request durations, accepted records and rejected records. A larger concurrency setting is useful only if it improves the output you can actually consume without exceeding the intended load or memory budget.

Run a pilot containing slow responses, a failed request and a deliberately slow output sink. Check whether the collector accumulates results while waiting for storage. Backpressure should reach the producer rather than merely moving an unbounded backlog into another stage.

Combine this scheduling boundary with bounded failure handling and record acceptance checks. Use a client setup guide for the connection; keep workload policy in the collector that owns the run.

If your URL list is already known, see Web Scraping API batch requests. Limit submissions while prior jobs still occupy your processing capacity.

Contact us

Tell us what you want to collect.

See our Privacy Notice for how we handle your message.

Your message is sent to hello@crawlhub.ai. Please do not include passwords or API keys.