Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback from delay in Celery

I am using django-celery. I need to download a large video file. I would like to update my database when the file has finished downloading. Is there a way to add a callback that will call django code, not another task, when the task has completed? My ideal code would look like this...

from video.tasks import video_download
from video.models import Video

def my_callback(v):    
    v.status = "downloaded"
    v.save()

def download_http(request):
   v = Video.objects.latest().id #this is a string
   a = video_download.delay(v, my_callback)

If there is another way to update an object after a celery task has been completed, I would be interested in that also.

PS: I tried passing in v = Video.objects.latest() instead of v = Video.objects.latest().id so I could just update the instance along the way, but celery did not like it because it was an object and not a string. Although it didn't throw any errors, every time I called a.ready it returned False.

like image 457
Alexis Avatar asked Jun 27 '26 08:06

Alexis


1 Answers

You can just call your Django code from the task. For example:

def video_download(v):
     from video.models import Video
     v = Video.objects.get(pk=v)
     do_download(v)
     v.status = "downloaded"
     v.save()
like image 165
Spc_555 Avatar answered Jun 30 '26 13:06

Spc_555



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!