Question

I have a django celery view which do the certain task and after the task has been completed successfully write that in database.

I am doing this:

result = file.delay(password, source12, destination)

And,

 if result.successful() is True:
      #writes into database

But after the task has finished execution it doesn't enter into the if condition.I tried with result.ready() but no luck.

Edit: Those above lines are in the same view:

def sync(request):
    """Sync the files into the server with the progress bar"""
    choice = request.POST.getlist('choice_transfer')
    for i in choice:
        source12 = source + '/' + i 
        start_date1 = datetime.datetime.utcnow().replace(tzinfo=utc)
        start_date = start_date1.strftime("%B %d, %Y, %H:%M%p")

        basename = os.path.basename(source12) #Get file_name
        extension = basename.split('.')[1] #Get the file_extension
        fullname = os.path.join(destination, i) #Get the file_full_size to calculate size

        result = file.delay(password, source12, destination)

        if result.successful() is True:
             #Write into database

e: #Writes to database

Was it helpful?

Solution

  1. When you call file.delay, celery queues up the task to run in the background, at some later point.

  2. If you immediately check result.successful(), it'll be false, as the task hasn't run yet.

If you need to chain tasks (one firing after another) use Celery's workflow solutions (in this case chain):

def do_this(password, source12, destination):
    chain = file.s(password, source12, destination) | save_to_database.s()
    chain()


@celery.task()
def file(password, source12, destination):
    foo = password
    return foo


@celery.task()
def save_to_database(foo):
    Foo.objects.create(result=foo)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top