[英]Celery: how to limit number of tasks in queue and stop feeding when full?
我對Celery很新,這是我的問題:
假設我有一個腳本經常被認為是從DB獲取新數據並使用Celery將其發送給工作人員。
tasks.py
# Celery Task
from celery import Celery
app = Celery('tasks', broker='amqp://guest@localhost//')
@app.task
def process_data(x):
# Do something with x
pass
fetch_db.py
# Fetch new data from DB and dispatch to workers.
from tasks import process_data
while True:
# Run DB query here to fetch new data from DB fetched_data
process_data.delay(fetched_data)
sleep(30);
這是我的擔憂:數據每30秒獲取一次。 process_data()函數可能需要更長的時間,並且取決於工作者的數量(特別是如果太少),隊列可能會受到我所理解的限制。
問題是如何設置隊列大小以及如何知道它已滿? 一般來說,如何應對這種情況?
您可以設置RabbitMQ的x-max-length
使用隊列預先聲明海帶
例如:
import time
from celery import Celery
from kombu import Queue, Exchange
class Config(object):
BROKER_URL = "amqp://guest@localhost//"
CELERY_QUEUES = (
Queue(
'important',
exchange=Exchange('important'),
routing_key="important",
queue_arguments={'x-max-length': 10}
),
)
app = Celery('tasks')
app.config_from_object(Config)
@app.task(queue='important')
def process_data(x):
pass
或使用政策
rabbitmqctl set_policy Ten "^one-meg$" '{"max-length-bytes":1000000}' --apply-to queues
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.