Skip to main content
The CrawlerEngine class is the core component that orchestrates the entire crawling process. It manages request scheduling, concurrency, rate limiting, checkpoint/resume functionality, and item collection.
This class is typically used internally by the Spider framework. You usually don’t instantiate it directly.

Class Definition

Constructor

Spider
required
The spider instance to run.
SessionManager
required
Session manager containing configured sessions.
str | Path | AsyncPath | None
default:"None"
Directory for checkpoint files. If None, checkpointing is disabled.
float
default:"300.0"
Seconds between periodic checkpoint saves (default 5 minutes). Set to 0 to disable periodic checkpoints.

Attributes

Spider
Reference to the spider being executed.
SessionManager
Session manager for handling requests.
Scheduler
Request scheduler with duplicate filtering.
CrawlStats
Current crawl statistics.
bool
Whether the crawl was paused (vs. completed normally).

Methods

crawl

Run the spider and return crawl statistics. This is the main entry point for the engine. Returns: CrawlStats object with detailed crawl metrics Process flow:
  1. Check for existing checkpoint and restore if found
  2. Call spider.on_start(resuming=bool)
  3. Generate initial requests from spider.start_requests() (if not resuming)
  4. Process requests concurrently with rate limiting
  5. Handle responses through callbacks
  6. Save periodic checkpoints (if enabled)
  7. Call spider.on_close() on completion
  8. Clean up checkpoint files on successful completion

request_pause

Request a graceful pause of the crawl.
  • First call: Requests graceful pause (waits for active tasks to complete)
  • Second call: Forces immediate stop (cancels active tasks)
This method is called automatically when the user presses Ctrl+C.

items

Access scraped items collected during the crawl. Returns: ItemList containing all scraped items

Internal Methods

_process_request

Download and process a single request. Handles:
  • Rate limiting (global and per-domain)
  • Download delay
  • Session fetching
  • Blocked request detection and retry
  • Callback execution
  • Item processing
  • Error handling

_save_checkpoint

Save current crawl state to checkpoint files. Includes:
  • Pending requests in the scheduler
  • Seen request fingerprints

_restore_from_checkpoint

Attempt to restore state from checkpoint. Returns: True if successfully restored, False if no checkpoint found

_is_domain_allowed

Check if the request’s domain is in spider.allowed_domains. Returns: True if allowed (or if allowed_domains is empty)

_normalize_request

Normalize request fields before enqueueing. Resolves empty sid to the default session ID.

Async Iteration

The engine supports async iteration for streaming items:
This is used internally by Spider.stream().

Usage Examples

Direct Engine Usage (Advanced)

Streaming Items

Monitoring Progress

Concurrency Control

The engine manages concurrency at two levels:

Global Concurrency

Implemented via CapacityLimiter - limits total active requests.

Per-Domain Concurrency

Implemented via per-domain CapacityLimiter - prevents overwhelming specific servers.

Download Delay

Applied before each request is fetched.

Checkpoint System

When crawldir is provided, the engine automatically saves checkpoints:

Checkpoint Timing

  1. Periodic saves: Every interval seconds (default 300)
  2. Graceful pause: When request_pause() is called
  3. SIGINT handler: Automatic on Ctrl+C

Checkpoint Contents

Checkpoints store:
  • Pending requests: All requests still in the scheduler queue
  • Seen fingerprints: Set of request fingerprints to avoid re-fetching

Resume Behavior

On resume:
  • Skips spider.start_requests()
  • Restores pending requests to scheduler
  • Continues from where it left off
  • Calls spider.on_start(resuming=True)

Error Handling

The engine handles errors at multiple levels:

Request Errors

Callback Errors

Blocked Request Handling

Performance Metrics

The engine tracks comprehensive statistics in CrawlStats:
  • requests_count: Total requests made
  • failed_requests_count: Failed requests
  • blocked_requests_count: Detected blocked requests
  • offsite_requests_count: Filtered offsite requests
  • items_scraped: Items yielded and accepted
  • items_dropped: Items dropped by on_scraped_item
  • response_bytes: Total bytes downloaded
  • domains_response_bytes: Per-domain bandwidth
  • sessions_requests_count: Requests per session
  • response_status_count: Status code distribution
  • elapsed_seconds: Total crawl duration
  • requests_per_second: Throughput rate

See Also