如何在执行celery任务时停止它并在一段时间后继续执行?(How to stop celery task while it is being executed and continue the execution after some time?)
我有以下芹菜任务(简化),它与Twitter API交互。
@app.task
def get_followers(screen_name, **kwargs):
cursor = kwargs.get('cursor', -1)
followers = kwargs.get('followers', [])
while True:
response = twitter_api.call('followers', 'ids', screen_name=screen_name, cursor=cursor)
if response.status_code == '429': # RATE LIMIT EXCEEDED
# do something here
cursor = response.json()['next_cursor']
if cursor == 0: # we're done
break
return followers
我希望能够在达到速率限制时暂停任务一段时间,并从停止的位置恢复执行。 (或者抛出错误并重试任务,传入额外的kwargs)。 如何实现这一目标?
I have the following celery task(simplified), which interacts with Twitter API.
@app.task
def get_followers(screen_name, **kwargs):
cursor = kwargs.get('cursor', -1)
followers = kwargs.get('followers', [])
while True:
response = twitter_api.call('followers', 'ids', screen_name=screen_name, cursor=cursor)
if response.status_code == '429': # RATE LIMIT EXCEEDED
# do something here
cursor = response.json()['next_cursor']
if cursor == 0: # we're done
break
return followers
I want to be able to pause task for some time when the rate limit is hit, and resume the execution from the point where it left off. (Or throw an error and retry the task, passing in additional kwargs). How can this be accomplished?
原文:https://stackoverflow.com/questions/40887865
2020-08-18 22:08
满意答案
您可以在捕获429错误代码时重试您的任务:
@app.task(bind=True)
def get_followers(self, screen_name, **kwargs):
cursor