如果直接以dataframe的形式传递到celery,会报如下的错:
InMemoryUploadedFile is not JSON serializable
在参考别的资料(https://cloud.tencent.com/developer/ask/sof/962149)后,将文件编码/解码为其Base64字符串表示形式。
import base64
import tempfile
# (Django, HTTP server)
file = request.FILES['files'].file
file_bytes = file.read()
file_bytes_base64 = base64.b64encode(file_bytes)
file_bytes_base64_str = file_bytes_base64.decode('utf-8') # this is a str
# (...send string through Celery...)
# (Celery worker task)
file_bytes_base64 = file_bytes_base64_str.encode('utf-8')
file_bytes = base64.b64decode(file_bytes_base64)
b = BytesIO(file_bytes2)
df_data = pd.read_csv(b, encoding='utf-8')