[英]NodeJS upload files 100% cpu usage
I've tried two different libraries: multer and formidable for handling file upload in node and both of them use 100% CPU during upload. 我尝试了两个不同的库: multer和formidable用于处理节点中的文件上传,并且它们在上载期间都使用100%CPU。
Is it a common node problem? 这是常见的节点问题吗? And how people deal with it in high concurrency environment?
人们如何在高并发环境中处理它?
Node version: v0.10.36
(I've even tried other versions like v0.11.x or v0.10.33 ) 节点版本:
v0.10.36
(我甚至尝试过其他版本,如v0.11.x或v0.10.33 )
Formidable example 强大的例子
Picture.upload = function(user, req, cb) {
var formidable = require('formidable')
var form = new formidable.IncomingForm();
form.uploadDir = "./uploads";
form.maxFieldsSize = app.settings.uploadMaxSize * 1024 * 1024;
form.maxFields = 1000;
form.parse(req, function(err, fields, files) {
cb(null, files);
});
}
Multer example Multer的例子
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
limits: {
files: 1,
fileSize: app.settings.uploadMaxSize * 1024 * 1024
}
})); // after I process the file from req.files
File are uploaded as multipart/form-data
. 文件作为
multipart/form-data
上传。
I'm using loopback , but I don't think it makes any difference. 我正在使用环回 ,但我认为它没有任何区别。
In a high concurrency environment if a task makes the cpu reach 100% sadly node will block . 在高并发环境中,如果任务使cpu达到100%,则遗憾的是节点将被阻塞 。 I assume uploading a file means the same thing as serving static assets.
我假设上传文件意味着与提供静态资产相同。 There is a lot of discussion here about how nodejs is not suited for serving static content.
有很多的讨论, 这里大约是的NodeJS如何不适合于提供静态内容。 Use nginx or apache instead.
请改用nginx或apache。
Another solution is to delegate the upload to a worker process or queuing system. 另一种解决方案是将上载委托给工作进程或排队系统。 See ZMQ for example.
例如,请参阅ZMQ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.