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

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • 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/joe...
    cd mojo-gallery
    git checkout youtube-video-cluster
    ```

ความคิดเห็น • 66

  • @razvangorgan2186
    @razvangorgan2186 3 ปีที่แล้ว +13

    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 🍻

  • @saeeduchiha5537
    @saeeduchiha5537 2 ปีที่แล้ว +3

    I respected that you came back to fix the video

  • @DnKZone
    @DnKZone ปีที่แล้ว

    You made it super easy!!
    Bet no one can teach clustering that much easy.
    Subscribed :)

  • @carlosjacobfield-sierra3759
    @carlosjacobfield-sierra3759 3 ปีที่แล้ว +1

    Great video man best one I found

    • @JoelCodes
      @JoelCodes  2 ปีที่แล้ว +1

      Thanks. Much appreciate the feedback. Cheers 🍻

  • @ankurrai2837
    @ankurrai2837 3 ปีที่แล้ว +1

    Love from India, Amazing

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว +1

      Hello friends from India! Cheers 🍻

    • @ankurrai2837
      @ankurrai2837 3 ปีที่แล้ว +1

      @@JoelCodes Your student 🙏🙏🙏

  • @leozou2537
    @leozou2537 3 ปีที่แล้ว +2

    Really helpful, thanks

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว

      I appreciate the feedback! Thanks!

  • @IamNobodysir
    @IamNobodysir 2 ปีที่แล้ว +1

    Anyone know how to get real user sys execute time in terminal? Somekind of plugin or extension?

  • @panicogalactico7379
    @panicogalactico7379 2 ปีที่แล้ว

    Awesome work!! Thank you!!

  • @BrettCooper4702
    @BrettCooper4702 4 ปีที่แล้ว +1

    Very nicely done, thanks

    • @JoelCodes
      @JoelCodes  4 ปีที่แล้ว

      I appreciate the feedback. Glad you found the video to be useful. Makes me happy. 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?

  • @timothyhallbeck9853
    @timothyhallbeck9853 3 ปีที่แล้ว +1

    Great lesson, thx

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว

      Appreciate the feedback! Hope to see you around. Cheers 🍻

  • @sudhir-pl6tz
    @sudhir-pl6tz ปีที่แล้ว

    Hi am confused between worker_threads and workerpools package
    could you please explain?

  • @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

  • @rahulbera454
    @rahulbera454 3 ปีที่แล้ว +1

    subscribed !!!

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว

      Thanks. Much appreciated 🙏

  • @mthaha2735
    @mthaha2735 3 ปีที่แล้ว +1

    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

  • @brunokotesky5965
    @brunokotesky5965 3 ปีที่แล้ว +1

    Very nice, do you know how can i debbug worker threads or clusters using typescript files on vscode?

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว +1

      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 🍻

  • @eatwithsadam1320
    @eatwithsadam1320 3 ปีที่แล้ว +1

    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.

  • @thewetfly9207
    @thewetfly9207 ปีที่แล้ว

    I am master, I am cluster. I am master of clusters 😉Like & sub, thanks for sharing.

  • @calvinrogercanas6737
    @calvinrogercanas6737 3 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@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. 😂

  • @valdemarjohannesson2720
    @valdemarjohannesson2720 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@JoelCodes thanks alot! Your vid helped me see why my workers didn't want to cooperate with me

    • @valdemarjohannesson2720
      @valdemarjohannesson2720 3 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

      @@JoelCodes Thanks alot. Do you have any video on worker-threads?

  • @christianaustria741
    @christianaustria741 3 ปีที่แล้ว +1

    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.

  • @mchannel1365
    @mchannel1365 3 ปีที่แล้ว +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 ปีที่แล้ว +1

      @@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 ปีที่แล้ว +1

    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.

  • @soniablanche5672
    @soniablanche5672 2 ปีที่แล้ว +1

    I think your cpu has multithreading and generally logical cores aren't as efficient as real cores

    • @JoelCodes
      @JoelCodes  2 ปีที่แล้ว

      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 🍻

  • @frontendsourcetom9428
    @frontendsourcetom9428 3 ปีที่แล้ว +1

    guess what , i subed for learning english rn

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว

      Awesome! Hope to see you around then. Cheers 🍻

  • @dungduong89
    @dungduong89 3 ปีที่แล้ว +1

    what is the name of the theme vscode you use

    • @JoelCodes
      @JoelCodes  3 ปีที่แล้ว +1

      Right now I am using a theme called Moonlight and the font FiraCode.

    • @dungduong89
      @dungduong89 3 ปีที่แล้ว

      @@JoelCodes thank bro

  • @sajad2126
    @sajad2126 ปีที่แล้ว

    360p ?

  • @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 ปีที่แล้ว +1

      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?