CLUSTER MULTI THREADED NODEJS -- Run threads in parallel to speed up long processes (CORRECTED)

  Рет қаралды 33,842

Joel Codes

Joel Codes

3 жыл бұрын

My JavaScript that creates thumbnails for hundreds of files is taking too long. So I have decided to speed things up with cluster.fork() that is built into Node.js. This will allow the workload to be spread out across my multi-core processor system.
⚠ 8:53 should read: index % forks === cluster.worker.id - 1
To follow along, you can fork this repository. I have added a tag you can check out that will bring you right to the point this video was created.
```
git checkout github.com/joelnet/mojo-galle...
cd mojo-gallery
git checkout youtube-video-cluster
```

Пікірлер: 66
@razvangorgan2186
@razvangorgan2186 3 жыл бұрын
Your content is straightforward without much bloatware. I respect that. Big up!
@JoelCodes
@JoelCodes 3 жыл бұрын
Thanks! I appreciate the feedback. I'm trying to create the content I would like to view. So I'm glad others are enjoying it also. Cheers 🍻
@DnKZone
@DnKZone Жыл бұрын
You made it super easy!! Bet no one can teach clustering that much easy. Subscribed :)
@saeeduchiha5537
@saeeduchiha5537 Жыл бұрын
I respected that you came back to fix the video
@JoelCodes
@JoelCodes Жыл бұрын
Cheers 🍻
@panicogalactico7379
@panicogalactico7379 Жыл бұрын
Awesome work!! Thank you!!
@carlosjacobfield-sierra3759
@carlosjacobfield-sierra3759 2 жыл бұрын
Great video man best one I found
@JoelCodes
@JoelCodes 2 жыл бұрын
Thanks. Much appreciate the feedback. Cheers 🍻
@BrettCooper4702
@BrettCooper4702 3 жыл бұрын
Very nicely done, thanks
@JoelCodes
@JoelCodes 3 жыл бұрын
I appreciate the feedback. Glad you found the video to be useful. Makes me happy. Cheers 🍻
@leozou2537
@leozou2537 3 жыл бұрын
Really helpful, thanks
@JoelCodes
@JoelCodes 3 жыл бұрын
I appreciate the feedback! Thanks!
@ankurrai2837
@ankurrai2837 3 жыл бұрын
Love from India, Amazing
@JoelCodes
@JoelCodes 3 жыл бұрын
Hello friends from India! Cheers 🍻
@ankurrai2837
@ankurrai2837 3 жыл бұрын
@@JoelCodes Your student 🙏🙏🙏
@timothyhallbeck9853
@timothyhallbeck9853 3 жыл бұрын
Great lesson, thx
@JoelCodes
@JoelCodes 3 жыл бұрын
Appreciate the feedback! Hope to see you around. Cheers 🍻
@bntheyoutube
@bntheyoutube Жыл бұрын
Great video! I am curious if you have a solution to pass a predetermined list to each thread? Instead of each thread having to process the entire list. Hopefully that makes sense... thoughts?
@rahulbera454
@rahulbera454 3 жыл бұрын
subscribed !!!
@JoelCodes
@JoelCodes 2 жыл бұрын
Thanks. Much appreciated 🙏
@sudhir-pl6tz
@sudhir-pl6tz Жыл бұрын
Hi am confused between worker_threads and workerpools package could you please explain?
@mthaha2735
@mthaha2735 3 жыл бұрын
Great content my man. Can you give some other way in which we can segregate the work to each worker other than using the filter method
@JoelCodes
@JoelCodes 3 жыл бұрын
I'm not sure I fully understand what you're asking. I have been thinking about making a part 2 for this, but I haven't got it all planned out yet. Still thinking about it.
@mthaha2735
@mthaha2735 3 жыл бұрын
@@JoelCodes There is a huge array and on every item there is some heavy computational task being performed. So in the video you are allocating different parts of the array to different process using the filter method of array. This is one way of distributing load. Can you give some other example other than using the filter method
@IamNobodysir
@IamNobodysir 2 жыл бұрын
Anyone know how to get real user sys execute time in terminal? Somekind of plugin or extension?
@thewetfly9207
@thewetfly9207 Жыл бұрын
I am master, I am cluster. I am master of clusters 😉Like & sub, thanks for sharing.
@calvinrogercanas6737
@calvinrogercanas6737 3 жыл бұрын
Hello Joel, can you explain why your code didn't get stuck when the worker id is 1? It means your filter will always pass since the calculation will always be 0? Thanks and love the video!
@calvinrogercanas6737
@calvinrogercanas6737 3 жыл бұрын
not stuck per se, but that worker did not get all the load?
@JoelCodes
@JoelCodes 3 жыл бұрын
What is the time stamp in question? I'll go through it and try to explain it better for you. Cheers 🍻
@calvinrogercanas6737
@calvinrogercanas6737 3 жыл бұрын
@@JoelCodes 9:47 line 45 -- let's say the first forks value is 1 so the result of modulo calculation will always be 0 and all of images will be included on that clusterFiles since it will pass the filter everytime. right?
@JoelCodes
@JoelCodes 3 жыл бұрын
@@calvinrogercanas6737 Yes you have got it correct. The cluster (index 1) would have processed the full load and cluster (index 2) would have processed all the evens, index 3 would have done every 3rd. Not the result I was looking for. 😂
@MuhammadImran-yr4pl
@MuhammadImran-yr4pl Жыл бұрын
you didn't give the source media file so i used some other photos as a source, but it's not working. though application is running but photo resizing mechanism is not working
@brunokotesky5965
@brunokotesky5965 3 жыл бұрын
Very nice, do you know how can i debbug worker threads or clusters using typescript files on vscode?
@JoelCodes
@JoelCodes 3 жыл бұрын
I'm still learning TypeScript, so I don't have any videos that cover TypeScript. But the process shouldn't be much different, other than setup. I'll put this one in my queue of videos to make. Great suggestion. Cheers 🍻
@christianaustria741
@christianaustria741 3 жыл бұрын
any update on how much speed it has saved if you use the corrected code?
@JoelCodes
@JoelCodes 3 жыл бұрын
The speed improvement will vary based on the application. So while I could have run benchmarks on this code, they would only apply to this specific code and not represent your app. It will have more effect with apps that use a lot of cpu.
@eatwithsadam1320
@eatwithsadam1320 3 жыл бұрын
Thanks to its Very helpful. How implement CRON side using Cluster.
@JoelCodes
@JoelCodes 3 жыл бұрын
Glad to hear you found this useful. Can you expand on what you mean about CRON?
@eatwithsadam1320
@eatwithsadam1320 3 жыл бұрын
@@JoelCodes When cron was running and a huge amount of data was being added, the site went down.
@frontendsourcetom9428
@frontendsourcetom9428 3 жыл бұрын
guess what , i subed for learning english rn
@JoelCodes
@JoelCodes 3 жыл бұрын
Awesome! Hope to see you around then. Cheers 🍻
@valdemarjohannesson2720
@valdemarjohannesson2720 3 жыл бұрын
Any possibility you can share code for this video? I've tried to follow the video and with your github project, but alas not working. It goes back and forth to quickly for me.
@JoelCodes
@JoelCodes 3 жыл бұрын
No problem! I put a tag in on the section of the code in the repository so you can go right to it. Here's the download link github.com/joelnet/mojo-gallery/releases/tag/youtube-video-cluster
@valdemarjohannesson2720
@valdemarjohannesson2720 3 жыл бұрын
@@JoelCodes thanks alot! Your vid helped me see why my workers didn't want to cooperate with me
@valdemarjohannesson2720
@valdemarjohannesson2720 3 жыл бұрын
@@JoelCodes Im not 100% on it, can you help me with the code? It looks like this right now, I removed resize from your file, but it aint firing away. hastebin.com/yifiqihayi.js
@JoelCodes
@JoelCodes 3 жыл бұрын
@@valdemarjohannesson2720 One thing I see in this code is you are awaiting `hugeData`, but `hugeData` is an Array. So you don't have to await that. You are also creating the function `makeGetRequest` inside of the for loop. Move this to outside of the for. Inside of `makeGetRequest` you are also referencing `i`, but it is not declared.
@valdemarjohannesson2720
@valdemarjohannesson2720 3 жыл бұрын
@@JoelCodes Thanks alot. Do you have any video on worker-threads?
@sajad2126
@sajad2126 Жыл бұрын
360p ?
@mchannel1365
@mchannel1365 3 жыл бұрын
But its still single thread ,multiple processes. Of course it increases performance as it uses multi core cpu , but you can't share memory within processes i.e you must use databases or external storage at all times , you can't use or share dynamic shared state with clustering module. Clustering is OK for http web based apps, but not necessarily for IOT , online multi user games or Big Data.
@JoelCodes
@JoelCodes 3 жыл бұрын
Messages can be passed between threads. I would disagree with staying it's use is only limited to web apps. There are many successful multi process js applications out there that prove this.
@mchannel1365
@mchannel1365 3 жыл бұрын
@@JoelCodes Yes I agree , I think what you are saying is about worker threads
@IgorogI1000
@IgorogI1000 3 жыл бұрын
That is a important difference... I got a bit confused because of that
@somerandomguy1533
@somerandomguy1533 3 жыл бұрын
can I use this to scale apps and/or games to multiple cores with multiple cpu's? Or am I just dreaming? lol
@JoelCodes
@JoelCodes 3 жыл бұрын
Ya you can definitely use this. I believe most game engines will automatically do this (multi-processor) for you though. So you don't have to manually offload work.
@somerandomguy1533
@somerandomguy1533 3 жыл бұрын
@@JoelCodes Well in the case that I'm using older hardware to run a modern games... For example star citizen. I have to use sde.exe ( Intel's software developer executable) to emulate the AVX instruction set because I use old hardware. And for that reason, I like to see what software improvements I can do to bring them up to speed. I think you see where I'm going with this, hopefully.
@dungduong89
@dungduong89 3 жыл бұрын
what is the name of the theme vscode you use
@JoelCodes
@JoelCodes 2 жыл бұрын
Right now I am using a theme called Moonlight and the font FiraCode.
@dungduong89
@dungduong89 2 жыл бұрын
@@JoelCodes thank bro
@soniablanche5672
@soniablanche5672 2 жыл бұрын
I think your cpu has multithreading and generally logical cores aren't as efficient as real cores
@JoelCodes
@JoelCodes Жыл бұрын
You may be right about that and I didn't think about that at the time. It would be nice to separate out the real cores and run a test on it. Next time I guess. Cheers 🍻
@dzenish.2262
@dzenish.2262 3 жыл бұрын
Dude, I'm sorry. But this is BAD. You really, and I mean really, need to delete or recreate this video.
@JoelCodes
@JoelCodes 3 жыл бұрын
Hi Dzenis, could you be more specific on what part of the video you would consider bad? Or an approach that you would recommend instead?
How to scale NodeJs applications using the cluster module.
13:37
Mafia Codes
Рет қаралды 49 М.
Они убрались очень быстро!
00:40
Аришнев
Рет қаралды 1,2 МЛН
КАКОЙ ВАШ ЛЮБИМЫЙ ЦВЕТ?😍 #game #shorts
00:17
Poopigirl
Рет қаралды 10 МЛН
Pray For Palestine 😢🇵🇸|
00:23
Ak Ultra
Рет қаралды 36 МЛН
PROOF JavaScript is a Multi-Threaded language
8:21
Beyond Fireship
Рет қаралды 261 М.
All you need to know about "child_process" in Node.js
12:38
Software Developer Diaries
Рет қаралды 2,6 М.
Scaling your Node.js app using the "cluster" module
13:44
Software Developer Diaries
Рет қаралды 33 М.
JavaScript Visualized - Event Loop, Web APIs, (Micro)task Queue
12:35
Build this JS calculator in 15 minutes! 🖩
15:20
Bro Code
Рет қаралды 358 М.
How to use Multithreading with "worker threads" in Node.js?
19:34
Software Developer Diaries
Рет қаралды 45 М.
Озвучка @patrickzeinali  Тюремная еда  Часть 2 @ChefRush
0:52
BigXep. Канал озвучки
Рет қаралды 534 М.
你们家里有这么调皮可爱的孩子吗? #袋鼠妈妈
0:41
袋鼠妈妈育儿记
Рет қаралды 14 МЛН
Озвучка @patrickzeinali  Тюремная еда  Часть 2 @ChefRush
0:52
BigXep. Канал озвучки
Рет қаралды 534 М.
A comical and humorous family
0:43
昕昕一家人
Рет қаралды 32 МЛН
GxK (New Empire) - Kong has a toothache  #godzillaxkongthenewempire
1:00