上一篇我们已经说过又拍云的常用操作,今天我们讲一下结合多线程实现异步上传
当我们使用第三方依赖,要注意模块是不是最新依赖,进行更新
UPYUN Python SDK文档:点击跳转
查看最新python SDK:点击跳转
多线程异步实现又拍云上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| import upyun import os import threading
up = upyun.UpYun('服务名称', '操作员名称', '操作员密码')
uploader = up.init_multi_uploader('/upyun_python/upyun_sanguo.mp4')
threads = []
t1 = threading.Thread(target=uploader.upload,args=(0,os.urandom(1024*1024)))
threads.append(t1) t2 = threading.Thread(target=uploader.upload,args=(1,os.urandom(1024*1024))) threads.append(t2)
for t in threads: t.start()
t.join()
res = uploader.complete()
|
多线程异步对文件进行分块上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Upyun_Post(APIView): def post(self,request): file = request.FILES.get('file') print(file) up = upyun.UpYun('服务名称', '操作员名称', '操作员密码') uploader = up.init_multi_uploader("/upyun-python-sdk/%s" % file, part_size=(1024 * 2048)) threads = [] for index, value in enumerate(file.chunks(chunk_size=(1024 * 2048))): t = threading.Thread(target=uploader.upload, args=(index, value)) threads.append(t) for i in threads: i.start() i.join() res = uploader.complete() return Response({'code':200,'message':'上传成功'})
|