简体   繁体   中英

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.

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 )

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

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 .

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 . 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. Use nginx or apache instead.

Another solution is to delegate the upload to a worker process or queuing system. See ZMQ for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM