4

It is said that Node.js code is single thread that should not processing some cpu-intensive tasks such image resizing, those tasks should be delegate to other programs such as ImageMagick other third party service.reference :

I have used S3-uploader lib in my project and test it using two devices to upload and resize images simultaneously, I found the server indeed is not blocked by one request of them, although the cpu usage rate is almost 100% and the response takes about 3000ms to finish(it is just a AWS EC2 t2.micro instance).

The understanding and questions are in the following image:

enter image description here

Please help me to ensure my understanding about node.js single thread above, and questions:

  1. When thread 1 delegate the task to ImageMagick, whether this thread is free to be used by new coming request or not?

Edit

  1. the image is not 100% correct. The delegate to and callback from ImageMagick should be through Event Loop as well and there is only one Event loop for a node application (My understanding, anyone can confirm?).

  2. answer from node-s3-uploader author: https://github.com/Turistforeningen/node-s3-uploader/issues/64

Damon Yuan
  • 3,683
  • 4
  • 23
  • 31

1 Answers1

3

Though ImageMagick might seem synchronous, it isn't. ImageMagick (and s3-uploader which uses im-resize) spawns a child process (using child_process.exec) in order to perform processing, thus it will not block the process.

As for what's' actually happening on your server, I assume from your description that you are running ImageMagick on your micro instance. The result is that while your process is free, meaning the event-loop isn't blocked, and events are being processed (question 1) and can handle other requests (question 2), ImageMagick is doing it's part in processing your image, and uses as much CPU as it gets. This in turn, does hog the CPU, which slows down your app.

This is a kind of ironic conflict: you are launching a separate process in order to free your thread, but that process actually steals all available CPU.

Best practice in this case is not to perform the heavy processing locally on the server, but delegate separate CPU for the task, whether it be an instance running a processing service, a lambda function, or any other solution that works for you.

Further reading:

Community
  • 1
  • 1
Selfish
  • 6,023
  • 4
  • 44
  • 63
  • Cool, I think upgrade can solve part of the high cpu usage problem, right? Although I feel that now that I am using AWS, I should use the Amazon Lambda service. Thanks a lot! – Damon Yuan Dec 01 '15 at 14:05
  • It may just be the time of day, but the first sentence of your answer was a little bit confusing to me; hopefully a bit of clarification can help. [s3-uploader](https://www.npmjs.com/package/s3-uploader) uses [im-resize](https://www.npmjs.com/package/im-resize) to resize images, which creates a child process (using [child_process.exec](https://github.com/Turistforeningen/node-im-resize/blob/master/index.js#L11), interestingly). The rest of your answer seems spot on. – Fissure King Dec 01 '15 at 14:20
  • DamonYuan yup, I tend to agree Lambda might be best practice. @FissureKing thanks! Good catch, It's a typo. I did mean 'spawns a child process'. Correcting. :) – Selfish Dec 01 '15 at 14:23