所以我正在使用Django + celery(rabbitMQ)。我编写了几个下载文件的任务,并发送请求以通知“嘿,我收到了文件”响应。我想并行运行这些任务。因此,我使用了芹菜的group
和链条组合。现在的主要问题是,当我运行任务时,它可以工作并且所有任务都被调用,而我只是无法检测到所有任务是否都完成了。是否有可能知道所有相关任务都已完成?
PS://我认为使用芹菜拍打不适合这项工作。因为执行芹菜拍打时当前的任务可能无法完成。
这是我的摘录,我首先调用此任务。
@shared_task(name="getOrders", bind=True, retry_backoff=10, autoretry_for=(exceptions.RequestException,), retry_kwargs={"max_retries": 3, "countdown": 5})
def getOrders(self):
url = "https://example.com/orders"
orders = fetchData(url) # fetch function
if orders.status_code == requests.codes.ok:
tree = etree.fromstring(orders.content)
jobs = group([download_order.si(order_id) for order_id in tree.xpath('order/text()')])
chain(jobs, notify.s()).apply_async()
self.retry(countdown=900, max_retries=2)
下载任务是:
@shared_task(name="download_order", acks_late=True, autoretry_for=(exceptions.RequestException,), retry_kwargs={"max_retries": 3, "countdown": 5},)
def download_order(order_id):
"""Download order"""
print("\n===========\nstarting download on {}\n=========\n".format(order_id))
url = "https://example.com/order/{}.xml".format(order_id)
get_xml = fetchData(url)
if fulfillment_request.status_code == request.codes.ok:
print("\n=======\nxml fetched\n========\n")
tree = etree.fromstring(get_xml.content)
for product in find_product(tree):
uuid = product.find("UUID").text
for v in product.iterfind("product"):
file_type = "Video"
file_id = v.find('files/file/id').text
url = "https://example.com/download?order_id={}&type={}&task_id={}".format(
order_id, file_type, file_id
)
path = "{}/{}/{}.mp4".format(
settings.VIDEO_DIRECTORY, order_id, file_id
)
chain(
download.si(url, path),
orderReceived.si(order_id, file_type, file_id)
).delay()
@shared_task(name="notify")
def notify(id):
print("notifying", id)
@shared_task(name="order_received")
def orderReceived(order_id, ftype, file_id):
print("order received", order_id, ftype, file_id)