【发布时间】:2020-05-19 04:02:47
【问题描述】:
我正在尝试在这里创建一个芹菜链:
chain(getAllProducts.s(shopname, hdrs),
editOgTags.s(title, description, whichImage, readableShopname, currentThemeId),
notifyBulkEditFinish.si(email, name, readableShopname, totalProducts),
updateBulkEditTask.si(taskID))()
在editOgTags中,有3个子任务:
@shared_task(ignore_result=True)
def editOgTags(products, title, description, whichImage, readableShopname, currentThemeId):
for product in products:
editOgTitle.delay(product, title, readableShopname)
editOgDescription.delay(product, description, readableShopname)
editOgImage.delay(product, int(whichImage), currentThemeId)
在每个editOgXXX函数中,都有一个函数被调用,有速率限制:
@shared_task(rate_limit='1/s')
def updateMetafield(index, loop_var, target_id, type_value):
resource = type_value + 's'
# print(f"loop_var key = {loop_var[index]['key']}")
if type_value == 'product' or type_value == 'collection' or type_value == 'article' or type_value == 'page':
meta = shopify.Metafield.find(resource=resource, resource_id=target_id, namespace='global', key=loop_var[index]['key'])
checkAndWaitShopifyAPICallLimit()
else:
print("Not available metafield type! Cannot update.")
return
if meta:
# meta[0].destroy()
meta[0].value = loop_var[index]['value']
meta[0].save()
else:
metafield = shopify.Metafield.create({
'value_type': 'string',
'namespace': 'global',
'value': loop_var[index]['value'],
'value-type': 'string',
'key': loop_var[index]['key'],
'resource': resource,
'resource_id': target_id,
})
metafield.save()
在漏桶算法下,一次提供40个api调用,2个reqs/s补货。由于 shopify 功能 的速率限制为 2 个请求/秒。我将速率限制设置为 1/s。当它用完 api 配额时,我会在 checkAndWaitShopifyAPICallLimit() 中调用 time.sleep(20) 等待补充。
问题是在所有任务完成之前调用电子邮件通知函数(notifyBulkEditFinish)。如何确保在所有任务完成后调用电子邮件功能?
我怀疑睡眠功能使任务落后于队列中的电子邮件功能。
【问题讨论】:
-
和弦就像一个组,但有一个回调。链原语让我们将签名链接在一起,以便一个接一个地调用,本质上形成一个回调链。把chain改成chord有什么区别?