0Pricing
Web Scraping & Bots · บทเรียน

การกระจายงานด้วยคิว

เรียนรู้ว่าคิวข้อความอย่าง Redis และ Celery ช่วยแยกการค้นพบ URL ออกจากการดึงข้อมูลได้อย่างไร เพื่อขยายการขูดเว็บไปยังผู้ปฏิบัติงานจำนวนมาก

การกระจายงานด้วยคิว เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Scraping & Bots และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Scaling Bottleneck

A single-process scraper is limited by one machine's CPU and network. To scale, you split work across many workers running in parallel, possibly on different servers.

A task queue is the glue that distributes work safely.

Producers and Consumers

The queue pattern has two roles:

  • Producers discover URLs and push tasks onto the queue.
  • Consumers (workers) pull tasks and fetch the pages.

Decoupling them lets each side scale independently.

A Redis-Backed Queue

Redis lists make a simple, fast queue. Producers LPUSH URLs; workers BRPOP them, blocking until work is available.

import redis
r = redis.Redis()

# producer
r.lpush('urls', 'https://site.com/page1')

# worker
_, url = r.brpop('urls')
print('processing', url)

Why Not Share a Python List

An in-memory list only works within one process. A Redis queue is shared across processes and machines, persists if a worker crashes, and handles concurrency atomically.

Introducing Celery

Celery is a full task framework built on a broker like Redis. You define tasks as functions and call them asynchronously; workers pick them up automatically.

from celery import Celery
app = Celery('scraper', broker='redis://localhost:6379/0')

@app.task
def scrape(url):
    return fetch_and_parse(url)

Dispatching Tasks

Calling .delay() enqueues the task and returns immediately. Workers running celery worker consume and execute them in parallel.

for url in discovered_urls:
    scrape.delay(url)

Retries and Failures

Celery can automatically retry failed tasks with backoff, so a transient network error does not lose a URL.

@app.task(bind=True, max_retries=3, default_retry_delay=10)
def scrape(self, url):
    try:
        return fetch_and_parse(url)
    except ConnectionError as e:
        raise self.retry(exc=e)

Deduplicating URLs

In distributed crawling the same URL can be discovered twice. Use a Redis set as a 'seen' filter so each page is fetched once.

if r.sadd('seen', url):
    scrape.delay(url)  # sadd returns 1 only if newly added

Backpressure and Concurrency

Tune worker concurrency to match target-site politeness and your bandwidth. Too many workers overwhelm the site; too few leave the queue backed up. Monitor queue length to find balance.

# start 4 worker processes
// celery -A scraper worker --concurrency=4

Results and Storage

Workers should write parsed data to a shared store (a database or object storage), not return it through the queue. The queue carries tasks; the datastore holds results.

Priority Queues

Not all URLs are equal. Route urgent tasks (a category index that unlocks many child pages) to a high-priority queue so workers handle them before low-value pages.

scrape.apply_async(args=[url], priority=9)  # higher runs sooner

Quick Check

Test your understanding of queue-based distribution.

Recap

You learned to scale scraping with task queues: the producer/consumer pattern, Redis-backed queues, Celery tasks with .delay(), automatic retries, URL deduplication with Redis sets, tuning concurrency, and writing results to shared storage.

คำถามที่พบบ่อย

บทเรียน “การกระจายงานด้วยคิว” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การกระจายงานด้วยคิว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การกระจายงานด้วยคิว”

เรียนรู้ว่าคิวข้อความอย่าง Redis และ Celery ช่วยแยกการค้นพบ URL ออกจากการดึงข้อมูลได้อย่างไร เพื่อขยายการขูดเว็บไปยังผู้ปฏิบัติงานจำนวนมาก คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การกระจายงานด้วยคิว” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม

ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การดึงข้อมูลแบบกระจายด้วย Scrapy
  2. Cloud Functions สำหรับการดึงข้อมูล
  3. การตรวจติดตามและการบันทึกการทำงาน
  4. การกระจายงานด้วยคิว
← กลับไปที่ Web Scraping & Bots