当前位置:网站首页>Divide and conquer, upload large files in pieces
Divide and conquer, upload large files in pieces
2022-07-28 21:50:00 【Ke Yulong】
Here we involve an algorithm —— Branching algorithm
In the recent advertising platform project , Because customers need to provide self certification videos , The certificate is the authenticity of my company .
Today, , The resolution of video is getting higher , Often accompanied by the huge video memory , A few minutes of video needs several g Of memory , It's definitely not possible to upload using ordinary methods , Large files are slow , Impact on customer experience , To solve this problem , I used the method of uploading in pieces , Will a big file , Divide into n Upload a small file , Speed up transmission efficiency !
Here we involve an algorithm —— Branching algorithm

The concept of fragment upload is the divide and conquer algorithm , Divide and rule , Put a big question , Turn into several small problems , Solve !!
Front end segmentation
First of all, we do it at the front , Fragment transmission to the back end , So here I'm going to use theta vue3,ui The framework is ant-desgin, The controls used are a-upload, Users click upload to select the file to upload .
Then we can get the name of the file , And the size , Then define the size you want to slice , Here you can write a judgment , It depends on the size of the file you want to upload , Define the size of your slice , If the file is too small , There is no need to slice , Occupancy resources . At this time, we can get the total number of films , Because there will be remainder , So what I use is Math.ceil Rounding up , Get the total number of films . At this time, we can get the start position and end position of each slice , And then through slice Slice the file , Send it to the backend for processing !
// Upload method
handleChange:function(file){
// Get file size 1024 byte 1kb
console.log(file.file)
var size = file.file.size
this.size = size
// this.filename = file.file.name
// Define fragment size
var shardSize = 1024 * 100
// The total number of films
// Rounding up
this.shardCount = Math.ceil(size / shardSize)
// Slicing operation
for (var i=0;i<this.shardCount; ++i){
// Starting position
var start = i * shardSize
// End position
var end = Math.min(size,start+shardSize)
// section
var shardfile = file.file.slice(start,end)
this.pushshard(shardfile,file.file.name,i)
}
},So far, the front-end slicing logic is completed !
Back end asynchronous write
Back end I use python Medium tronado The framework writes asynchronously , To avoid blocking caused by synchronous writing , install aiofiles The library cooperates tornado Asynchronous mechanism of , It can improve the efficiency of writing
Here, the back end gets the file entity , file name , Sign a , First write the file entity asynchronously to the specified path , And then read it , Merge in order , I won't talk too much about file operation , They are all fixed grammars , There's no logic , The following code shows , Please watch for yourself
# Patch uploading
class SliceUploadHandler(BaseHandler):
# Merging and slicing
async def put(self):
filename = self.get_argument("filename",None)
count = 0
size = self.get_argument("size",None)
try:
filesize = os.path.getsize("./static/upload/{}".format(filename))
except Exception as e:
print(str(e))
filesize = 0
if int(size) != filesize:
# Open file handle asynchronously
async with aiofiles.open("./static/upload/{}".format(filename),"ab") as file:
while True:
try:
# Read slices
shard_file = open("./static/upload/{}_{}".format(filename,count),"rb")
# Asynchronous write
await file.write(shard_file.read())
# Close the handle manually
shard_file.close()
except Exception as e:
print(str(e))
break
count = count + 1
self.finish({"errcode":0,"msg":" The merger is over "})
# Fragment file receiving
async def post(self):
# Receive fragment entities
file = self.request.files["file"][0]
# Get subscript
count = self.get_argument("count",None)
# Get the file name
filename = self.get_argument("filename",None)
# Get file content
content = file["body"]
# Asynchronous write
async with aiofiles.open("./static/upload/{}_{}".format(filename,count),"wb") as file:
# asynchronous
await file.write(content)
self.finish({"errcode":0,"msg":" Slice upload succeeded "})
So far, the debugging of the front and rear ends is completed , besides , In a real super large file transfer scenario , Due to network or other factors , It is likely to cause the fragmentation task to be interrupted , At this point, you need to respond quickly through degradation , Back to the bottom data , Avoid users' long waiting , Here we use a method based on Tornado Of Apscheduler Library to schedule fragment tasks .
边栏推荐
- Matlab|基础知识总结一
- What is the purpose of database read-write separation [easy to understand]
- Using El date picker to report errors in sub components
- An end-to-end aspect level emotion analysis method for government app reviews based on brnn
- Paging function (board)
- Timing analysis and constraints based on Xilinx
- 30. Learn highcharts label rotation histogram
- Bully is filed for bankruptcy! The company has become a "Lao Lai", and the legal person is restricted from high consumption
- World Hepatitis Day | grassroots can also enjoy the three a resources. How can the smart medical system solve the "difficulty of seeing a doctor"?
- How to skillfully use assertion + exception handling classes to make the code more concise! (glory Collection Edition)
猜你喜欢

PyQt5快速开发与实战 5.4 网页交互

OA项目之会议通知(查询&是否参会&反馈详情)

纳米金偶联抗体/蛋白试剂盒(20nm,1mg/100μg/500 μg偶联量)的制备

基于对象的实时空间音频渲染丨Dev for Dev 专栏

基于知识元的外文专利文献知识描述框架

Pytorch学习记录(三):随机梯度下降、神经网络与全连接

Cross domain transfer learning of professional skill word extraction in Chinese recruitment documents

融合LSTM与逻辑回归的中文专利关键词抽取

微信小程序开发入门,自己开发小程序

How to skillfully use assertion + exception handling classes to make the code more concise! (glory Collection Edition)
随机推荐
高举5G和AI两面旗帜:紫光展锐市场峰会火爆申城
Paging function (board)
MSI Bao'an factory is on fire! Official response: no one was injured, and the production line will not be affected!
Cy3/Cy5/Cy5.5/Cy7荧光标记抗体/蛋白试剂盒(10~100mg标记量)
Modify the port number of MySQL (is there a problem modifying the port number of MySQL)
磷脂偶联抗体/蛋白试剂盒的存储与步骤
LeetCode·581.最短无序连续子数组·双指针
For the next generation chromebook, MediaTek launched new chipsets mt8192 and mt8195
An end-to-end aspect level emotion analysis method for government app reviews based on brnn
Matlab|基础知识总结一
Why does Baidu search only crawl, but not show the page?
Week 6 Linear Models for Classification (Part B)
Storage and steps of phospholipid coupled antibody / protein Kit
Pytoch learning record (III): random gradient descent, neural network and full connection
数据库读写分离目的是做什么[通俗易懂]
Knowledge description framework of foreign patent documents based on knowledge elements
Bus, protocol, specification, interface, data acquisition and control system in industrial communication field
Leetcode linked list question - interview question 02.07. linked list intersection (learn linked list by one question and one article)
微信小程序开发入门,自己开发小程序
Cloud security core technology