| name | python-performance-optimization |
| description | Performance optimization patterns for Python scraper pipelines — connection pooling, memory efficiency, async I/O, and batch operations for high-throughput data ingestion. |
Python Performance Optimization for Scrapers
Connection Pooling
session = requests.Session()
session.headers.update(HEADERS)
Memory Efficiency
def process_records(records):
for record in records:
yield normalize(record)
class ArrestRecord:
__slots__ = ['name', 'booking_number', 'dob', 'charges', 'bond']
Batch Google Sheets Writes
for record in records:
sheet.append_row(record)
sheet.batch_update([{
'range': f'{tab}!A2',
'values': [record_to_row(r) for r in records]
}])
Deduplication with Sets
existing_ids = set(sheet.col_values(booking_number_col))
new_records = [r for r in records if r['Booking_Number'] not in existing_ids]
Parallel Scraping (When Safe)
from concurrent.futures import ThreadPoolExecutor
def scrape_detail_page(url):
return session.get(url, timeout=30).text
with ThreadPoolExecutor(max_workers=3) as executor:
results = list(executor.map(scrape_detail_page, detail_urls))
Profiling
time python counties/lee/runner.py --days-back 1
python -m cProfile -s cumulative counties/lee/runner.py
Key Metrics to Monitor
- Scrape duration (target: <2 min per county)
- Records per run (compare against historical average)
- API quota usage (Sheets API: 300 req/min)
- Memory usage (target: <256MB per scraper)