It is possible to have multiple threads on nodejs? I am using expressjs and multer for image upload. When an image have big size and webpage needs another request to be accomplished, it waits until image get uploaded. It's possible to make image upload use other thread?
-
As far as i know, node.js is single threaded. You can't have another thread do some work for you. However you can send another request to the server as the uploading work happens asynchronously and your request will be processed along with uploading function.giri-sh– giri-sh2015-09-14 10:10:59 +00:00Commented Sep 14, 2015 at 10:10
-
I am not sure i understood you. Upload image indeed upload asynchronously but in time i start to upload image i can't send other request. P.S. i am using this plugin: ng-file-uploadHonchar Denys– Honchar Denys2015-09-14 10:16:55 +00:00Commented Sep 14, 2015 at 10:16
-
Do you get any error or your next request is lost or next request gets processed only after image is uploaded?giri-sh– giri-sh2015-09-14 10:44:08 +00:00Commented Sep 14, 2015 at 10:44
-
it's not lost, there are no errors, it waits until image get uploaded and then make the request.Honchar Denys– Honchar Denys2015-09-14 11:23:11 +00:00Commented Sep 14, 2015 at 11:23
-
If your application is CPU-bound, you can use Socketnaut (multithreading) or Cluster (multiprocessing) to scale the main module. Socketnaut has an example specific to Express apps.stackhatter– stackhatter2023-09-21 16:56:46 +00:00Commented Sep 21, 2023 at 16:56
1 Answer
Node.js focuses on asynchronous processing. It is capable of accepting an image upload and servicing other requests at the same time. If it is not doing that it is because your code is doing some kind of synchronous processing. There is a cluster module for running the same node.js code across multiple processes (rather than threads); but it is not what you want. You may find it interesting to understand the difference between concurrency and parallelism. That article is focussed on the Go language so you don't get hung up on node.js specifics. You don't need another thread - you need to better understand the way node.js works. Perhaps you could post the relevant parts of your code in another question for more specific feedback.