Django Celery 可中断任务用法
我正在尝试使用Celery的可中断任务功能,但文档中的示例对我来说似乎不起作用。给出的示例是:
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
还有
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()
...
但是,我遇到了这个错误:
TypeError: MyLongRunningTask() takes exactly 1 argument (0 given)
我哪里出错了?
1 个回答
2
我只是猜测,但我觉得应该是
class MyLongRunningTask(AbortableTask)
而不是
def MyLongRunningTask(AbortableTask)