简体   繁体   中英

Canceling a celery task from inside itself?

I know I could return , but I'm wondering if there's something else, especially for helper methods where the task where return None would force the caller to add boilerplate checking at each invocation.

I found InvalidTaskError , but no real documentation - is this an internal thing? Is it appropriate to raise this?

I was looking for something like a self.abort() similar to the self.retry() , but didn't see anything.

Here's an example where I'd use it.

def helper(task, arg):
    if unrecoverable_problems(arg):
        # abort the task
        raise InvalidTaskError()

@task(bind=True)
task_a(self, arg):
    helper(task=self, arg=arg)
    do_a(arg)

@task(bind=True)
task_b(self, arg):
    helper(task=self, arg=arg)
    do_b(arg)

After doing more digging, I found an example using Reject ;

(copied from doc page)

The task may raise Reject to reject the task message using AMQPs basic_reject method. This will not have any effect unless Task.acks_late is enabled.

Rejecting a message has the same effect as acking it, but some brokers may implement additional functionality that can be used. For example RabbitMQ supports the concept of Dead Letter Exchanges where a queue can be configured to use a dead letter exchange that rejected messages are redelivered to.

Reject can also be used to requeue messages, but please be very careful when using this as it can easily result in an infinite message loop.

Example using reject when a task causes an out of memory condition:

 import errno from celery.exceptions import Reject @app.task(bind=True, acks_late=True) def render_scene(self, path): file = get_file(path) try: renderer.render_scene(file) # if the file is too big to fit in memory # we reject it so that it's redelivered to the dead letter exchange # and we can manually inspect the situation. except MemoryError as exc: raise Reject(exc, requeue=False) except OSError as exc: if exc.errno == errno.ENOMEM: raise Reject(exc, requeue=False) # For any other error we retry after 10 seconds. except Exception as exc: raise self.retry(exc, countdown=10) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM