开发者

Django Celery AbortableTask usage

开发者 https://www.devze.com 2023-01-14 22:56 出处:网络
I\'m trying to use the AbortableTask feature of Celery but the documentation example doesn\'t seem to be working for me.The e开发者_JS百科xample given is:

I'm trying to use the AbortableTask feature of Celery but the documentation example doesn't seem to be working for me. The e开发者_JS百科xample given is:

from celery.contrib.abortable import AbortableTask

def MyLongRunningTask(AbortableTask):

    def run(self, **kwargs):
        logger = self.get_logger(**kwargs)
        results = []
        for x in xrange(100):
            # Check after every 5 loops..
            if x % 5 == 0:  # alternatively, check when some timer is due
                if self.is_aborted(**kwargs):
                    # Respect the aborted status and terminate
                    # gracefully
                    logger.warning("Task aborted.")
                    return None
            y = do_something_expensive(x)
            results.append(y)
        logger.info("Task finished.")
        return results

and

from myproject.tasks import MyLongRunningTask

def myview(request):

    async_result = MyLongRunningTask.delay()
    # async_result is of type AbortableAsyncResult

    # After 10 seconds, abort the task
    time.sleep(10)
    async_result.abort()

    ...

However, I am getting the error:

TypeError: MyLongRunningTask() takes exactly 1 argument (0 given)

What am I doing wrong?


Just a guess but I think it should be

class MyLongRunningTask(AbortableTask)

and not

def MyLongRunningTask(AbortableTask)
0

精彩评论

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