开发者

Celery Task that comunicate with Twitter

What is the right approach when writing celery tasks that communicate with service that have a rate limits and sometimes is missing (not responding) for a long time of period?

Do I have to use task retry? What if service is missing too much time? Is there a way to store this tasks for later execution after a long time period?

What if this 开发者_JAVA百科is a subtask in a long task?


First, i suggest you to set a socket timeout for avoid long waiting of a response. Than you can catch the socket TimeOutException and in this particoular case to a retry with big amount of time, like 15 minutes. Anyway normally I use an incrementalRetry with a percentage of increment, this will increase the time every time the task retry, this is useful when you write task that depends on external services that can be available for long time. You can set on task an high number of retry like 50 and than set the standard retry time by using the var

#20 seconds
self.default_retry_delay = 20 

after you can implement a method like this for your task

def incrementalRetry(self, exc, perc = 20, args = None):
    """By default the retry delay is increased by 20 percent"""
    if args:
        self.request.args = args

    delay = self.default_retry_delay

    if self.request.kwargs.has_key('retry_deleay'):
        delay = self.request.kwargs['retry_deleay']

    retry_delay = delay+round((delay*perc)/100,2)
    #print "delay"+str(retry_delay)

    self.retry(self.request.args,
               self.request.kwargs.update({'retry_deleay':retry_delay}),
               exc=exc,countdown=retry_delay, max_retries=self.max_retries)

What if this is a subtask in a long task?

If you don't need the result you can launch it in async mode by using task.delay(args=[]) A nice feature is also the task group that allow you to launch different tasks and after all are finished you can to something else in you work flow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜