Files
nexus/wiki/concepts/异步任务处理.md
2026-04-14 16:02:50 +08:00

1.5 KiB
Raw Blame History

title, type, tags, sources, last_updated
title type tags sources last_updated
异步任务处理 concept
async
task-queue
django-q
tiktok-pm-python-django-project.md
2026-04-14

Definition

异步任务处理将耗时的操作如API调用、数据导入放到后台任务队列中执行避免HTTP请求超时。

Implementation: Django-Q

Configuration

Q_CLUSTER = {
    'name': 'DjangORM',
    'workers': 4,
    'timeout': 360,
    'retry': 120,
    'queue_limit': 50,
    'orm': 'default',
}

Task Function

from django_q.tasks import async_task

def trigger_bright_data_task(urls_list):
    # 触发Bright Data API
    job_id = trigger_bright_data_api(urls_list)
    # 异步轮询结果
    async_task('products.tasks.poll_bright_data_result', job_id)
    return job_id

def poll_bright_data_result(job_id):
    # 轮询直到任务完成
    while True:
        status = check_job_status(job_id)
        if status == 'completed':
            download_and_import_data()
            break
        time.sleep(30)

Worker

启动后台worker处理异步任务

docker compose exec web python manage.py qcluster

Bright Data Integration

  • 触发异步任务获取job_id
  • 每30秒轮询任务状态
  • 任务完成后下载JSON数据并导入数据库

Connections