How can a celery task accept a list of files as an argument?

Actually, I don't know how I could make the task have a list of files as an argument.

files = {}
   for file in request.FILES.getlist('files'):
      files[file.name] = file.file.read()

my_celery_task.apply_async(
   kwargs={
      'files': files,
   }
)

This throws the error 'Object of type bytes is not JSON serializable'.

I also tried to add these files to the constructor of a class, but it's still not good because the object cannot be serialized.

If I use files[file.name] = file, throws InMemoryUploadedFile is not JSON serializable, or if I use files[file.name] = file.file.read().decode('utf-8') => 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

I actually have no idea how to solve it. I need that dictionary of the form name: content as an argument given to the task

You can't pass a binary file as argument to a celery task. You need to store it on the filesystem (or S3 or another storage) and then pass the path to the files as argument.

Back to Top