如何限制imagemin-mozjpeg中的子进程数?

时间:2018-04-20 04:33:08

标签: node.js compression imagemin mozjpeg

我正在使用imagemin-mozjpeg,它使用mozjpeg二进制来压缩图像。

问题是我在nodejs webserver中使用它。

这就是现在的工作方式:

  1. 我正在使用“请求”模块(fs.createReadStream)上传JPEG图像。

  2. Multer处理流的流并将其保存在缓冲区(内存存储器)中。

  3. 然后将缓冲区传递给imagemin进行压缩。

  4. 然后将压缩缓冲区写入文件。 (example.jpg)

  5. 一切正常。

    这里的问题是,对于每个请求,都会生成一个新的mozjpeg二进制子进程cjpeg。

    1个子进程占用12.5 MB内存(对于.5 MB文件)。

    如果我同时发出50个请求,则大约为700 MB,因为对于50个图像,有50个子进程。

    有没有办法可以限制子进程的数量? (该库正在使用“execa”模块)或只生成4-5子进程,它们会对所有请求进行压缩。

    由于

    if (req.files.myimage[0].buffer) {
                let fileNumber = filesUploaded++;
    
                imagemin.buffer(req.files.myimage[0].buffer, {
                    plugins: [
                        imageminMozjpeg({ quality: 60, progressive: true })
                    ]
                })
                    .then(file => {
                        fs.writeFile(__dirname + '/uploads/' + uuidv4() + '.jpg', file, 'hex' , function () {
                            res.end("SUCCESS" + fileNumber.toString());
                        });
    
                    })
                    .catch(err => {
                        console.log(err);
                        res.end("FAILED");
                    });
    
            }
    

1 个答案:

答案 0 :(得分:0)

解决此问题的主要概念是限制对imagemin()(谁产生图像处理过程)的调用次数。

也许您可以使用任务队列来实现任务调度系统来收集请求,而某些工作人员可以使用imagemin()来处理请求。

    var multer  = require('multer')
    var upload = multer({ dest: 'your/uploads/' })

    // TaskScheduler, a wrapper of a task array
    class TaskScheduler extends Array {
      constructor (MAX_SLOTS, worker) {
        super()
        this._MAX_SLOTS= MAX_SLOTS
        this._busy_slots= 0
        this._worker= worker
      }

      /**
       * Create new tasks
       */
      push (...tasks) {
        const ret = super.push(...tasks)

        this.run()
        return ret
      }

      /**
       * Run tasks in available slots
       */
      run () {
        // if there are any tasks and available slots
        while (this.length > 0 && this._busy_slots < this._MAX_SLOTS) {
          const firstTask = this.shift()
          this._worker(firstTask).then(() => {
            // release the slot
            this._busy_slots--

            // since a task slot is released
            // call run() again to process another task from the queue
            this.run()
          })
          this._busy_slots++
        }
      }
    }

    // worker is supposed to return a Promise
    const scheduler = new TaskScheduler(5, function (task) {
      return imagemin.buffer(task.buffer, { /* imagemin options */ })
        .then(() => { /* write image files */ })
        .catch(() => { /* error handling */ })
    })

    // schedule the task when there is an incoming request
    // the actual code depends on your web server
    // here what to do in the callback is the point ;)

    // Take express for example, `app` is the express.Application
    app.post('your/end/point', upload.fields([{ name: 'myimage' }]), function (req) {
      if (req.files.myimage[0]) {
        scheduler.push(req.files.myimage[0])
      }
    })

请注意,由于您的计划程序是从Array扩展而来的,因此您可以使用任何Array方法来管理您的任务,例如pop()放弃上一个任务,shift()放弃第一个任务,unshift(newTask)在调度程序队列开头插入新任务。

相关问题