Can nodejs worker threads be used for executing long running file I/O based javascript code?











up vote
2
down vote

favorite
1












I can see that NodeJS is brining in multi-threading support via its worker threads module. My current assumption (I have not yet explored personally) is that I can offload a long running /cpu intensive operation to these worker threads.



I want to understand the behaviour if this long running piece of code has some intermittent event callbacks or chain of promises. Do these callbacks still execute on the worker threads, or do they get passed on back to the main thread?



If these promises come back to main thread, the advantage of executing the worker thread may be lost.



Can someone clarify..?



Update => Some context of the question



I have a http req that initiates some background processing and returns a 202 status. After receiving such request, I am starting a background processing via



setTimeout (function() { // performs long running file read operations.. })


and immediately return a 202 to the caller.



However, I have observed that, during this time while this background operation is going, other http requests are either not being processed, or very very sluggish at the best.



My hypothesis is that this continuous I/O processing of a million+ lines is filling up the event loop with callbacks / promises that the main thread is unable to process other pending I/O tasks such as accepting new requests.



I have explored the nodejs cluster option and this works well, as the long task is delegated to one of the child processes, and other instances of cluster are available to take up additional requests.



But I was thinking that worker threads might solve the same problem, without the overhead of cloning the process.










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    I can see that NodeJS is brining in multi-threading support via its worker threads module. My current assumption (I have not yet explored personally) is that I can offload a long running /cpu intensive operation to these worker threads.



    I want to understand the behaviour if this long running piece of code has some intermittent event callbacks or chain of promises. Do these callbacks still execute on the worker threads, or do they get passed on back to the main thread?



    If these promises come back to main thread, the advantage of executing the worker thread may be lost.



    Can someone clarify..?



    Update => Some context of the question



    I have a http req that initiates some background processing and returns a 202 status. After receiving such request, I am starting a background processing via



    setTimeout (function() { // performs long running file read operations.. })


    and immediately return a 202 to the caller.



    However, I have observed that, during this time while this background operation is going, other http requests are either not being processed, or very very sluggish at the best.



    My hypothesis is that this continuous I/O processing of a million+ lines is filling up the event loop with callbacks / promises that the main thread is unable to process other pending I/O tasks such as accepting new requests.



    I have explored the nodejs cluster option and this works well, as the long task is delegated to one of the child processes, and other instances of cluster are available to take up additional requests.



    But I was thinking that worker threads might solve the same problem, without the overhead of cloning the process.










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      I can see that NodeJS is brining in multi-threading support via its worker threads module. My current assumption (I have not yet explored personally) is that I can offload a long running /cpu intensive operation to these worker threads.



      I want to understand the behaviour if this long running piece of code has some intermittent event callbacks or chain of promises. Do these callbacks still execute on the worker threads, or do they get passed on back to the main thread?



      If these promises come back to main thread, the advantage of executing the worker thread may be lost.



      Can someone clarify..?



      Update => Some context of the question



      I have a http req that initiates some background processing and returns a 202 status. After receiving such request, I am starting a background processing via



      setTimeout (function() { // performs long running file read operations.. })


      and immediately return a 202 to the caller.



      However, I have observed that, during this time while this background operation is going, other http requests are either not being processed, or very very sluggish at the best.



      My hypothesis is that this continuous I/O processing of a million+ lines is filling up the event loop with callbacks / promises that the main thread is unable to process other pending I/O tasks such as accepting new requests.



      I have explored the nodejs cluster option and this works well, as the long task is delegated to one of the child processes, and other instances of cluster are available to take up additional requests.



      But I was thinking that worker threads might solve the same problem, without the overhead of cloning the process.










      share|improve this question















      I can see that NodeJS is brining in multi-threading support via its worker threads module. My current assumption (I have not yet explored personally) is that I can offload a long running /cpu intensive operation to these worker threads.



      I want to understand the behaviour if this long running piece of code has some intermittent event callbacks or chain of promises. Do these callbacks still execute on the worker threads, or do they get passed on back to the main thread?



      If these promises come back to main thread, the advantage of executing the worker thread may be lost.



      Can someone clarify..?



      Update => Some context of the question



      I have a http req that initiates some background processing and returns a 202 status. After receiving such request, I am starting a background processing via



      setTimeout (function() { // performs long running file read operations.. })


      and immediately return a 202 to the caller.



      However, I have observed that, during this time while this background operation is going, other http requests are either not being processed, or very very sluggish at the best.



      My hypothesis is that this continuous I/O processing of a million+ lines is filling up the event loop with callbacks / promises that the main thread is unable to process other pending I/O tasks such as accepting new requests.



      I have explored the nodejs cluster option and this works well, as the long task is delegated to one of the child processes, and other instances of cluster are available to take up additional requests.



      But I was thinking that worker threads might solve the same problem, without the overhead of cloning the process.







      node.js multithreading






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 13:08

























      asked Nov 9 at 13:25









      Mopparthy Ravindranath

      93921634




      93921634
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          I assume each worker thread would have its own event loop.



          So if you emit an event in a worker thread, only that thread would receive it and trigger the callback. The same for promises, if you create a promise within a worker, it will only be resolved by that worker.



          This is supported by their statement in the documentation regarding Class: Worker: Most Node.js APIs are available inside of it (with some exceptions that are not related to event processing).



          However they mention this earlier in the docs:




          Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can.




          I think some small scale async code in worker threads would be fine, but having more callbacks/promises would hurt performance. Some benchmarks could shed some light on this.






          share|improve this answer





















          • I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
            – Mopparthy Ravindranath
            Nov 11 at 12:43










          • So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
            – Mopparthy Ravindranath
            Nov 11 at 13:03






          • 1




            I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
            – mihai
            Nov 11 at 13:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226581%2fcan-nodejs-worker-threads-be-used-for-executing-long-running-file-i-o-based-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote













          I assume each worker thread would have its own event loop.



          So if you emit an event in a worker thread, only that thread would receive it and trigger the callback. The same for promises, if you create a promise within a worker, it will only be resolved by that worker.



          This is supported by their statement in the documentation regarding Class: Worker: Most Node.js APIs are available inside of it (with some exceptions that are not related to event processing).



          However they mention this earlier in the docs:




          Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can.




          I think some small scale async code in worker threads would be fine, but having more callbacks/promises would hurt performance. Some benchmarks could shed some light on this.






          share|improve this answer





















          • I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
            – Mopparthy Ravindranath
            Nov 11 at 12:43










          • So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
            – Mopparthy Ravindranath
            Nov 11 at 13:03






          • 1




            I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
            – mihai
            Nov 11 at 13:34















          up vote
          1
          down vote













          I assume each worker thread would have its own event loop.



          So if you emit an event in a worker thread, only that thread would receive it and trigger the callback. The same for promises, if you create a promise within a worker, it will only be resolved by that worker.



          This is supported by their statement in the documentation regarding Class: Worker: Most Node.js APIs are available inside of it (with some exceptions that are not related to event processing).



          However they mention this earlier in the docs:




          Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can.




          I think some small scale async code in worker threads would be fine, but having more callbacks/promises would hurt performance. Some benchmarks could shed some light on this.






          share|improve this answer





















          • I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
            – Mopparthy Ravindranath
            Nov 11 at 12:43










          • So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
            – Mopparthy Ravindranath
            Nov 11 at 13:03






          • 1




            I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
            – mihai
            Nov 11 at 13:34













          up vote
          1
          down vote










          up vote
          1
          down vote









          I assume each worker thread would have its own event loop.



          So if you emit an event in a worker thread, only that thread would receive it and trigger the callback. The same for promises, if you create a promise within a worker, it will only be resolved by that worker.



          This is supported by their statement in the documentation regarding Class: Worker: Most Node.js APIs are available inside of it (with some exceptions that are not related to event processing).



          However they mention this earlier in the docs:




          Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can.




          I think some small scale async code in worker threads would be fine, but having more callbacks/promises would hurt performance. Some benchmarks could shed some light on this.






          share|improve this answer












          I assume each worker thread would have its own event loop.



          So if you emit an event in a worker thread, only that thread would receive it and trigger the callback. The same for promises, if you create a promise within a worker, it will only be resolved by that worker.



          This is supported by their statement in the documentation regarding Class: Worker: Most Node.js APIs are available inside of it (with some exceptions that are not related to event processing).



          However they mention this earlier in the docs:




          Workers are useful for performing CPU-intensive JavaScript operations; do not use them for I/O, since Node.js’s built-in mechanisms for performing operations asynchronously already treat it more efficiently than Worker threads can.




          I think some small scale async code in worker threads would be fine, but having more callbacks/promises would hurt performance. Some benchmarks could shed some light on this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 22:48









          mihai

          22.9k73968




          22.9k73968












          • I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
            – Mopparthy Ravindranath
            Nov 11 at 12:43










          • So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
            – Mopparthy Ravindranath
            Nov 11 at 13:03






          • 1




            I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
            – mihai
            Nov 11 at 13:34


















          • I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
            – Mopparthy Ravindranath
            Nov 11 at 12:43










          • So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
            – Mopparthy Ravindranath
            Nov 11 at 13:03






          • 1




            I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
            – mihai
            Nov 11 at 13:34
















          I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
          – Mopparthy Ravindranath
          Nov 11 at 12:43




          I was planning on use a large file reading (line-by-line) on one of the worker threads. It seems, all the line reader events are filling the event loop, so no other http requests are being accepted during this time, leading to client timeout. This is the context why I wanted to use a worker thread.
          – Mopparthy Ravindranath
          Nov 11 at 12:43












          So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
          – Mopparthy Ravindranath
          Nov 11 at 13:03




          So, I am still in doubt whether a worker thread is the right use case for doing a long running I/O. My initial experiment with (github.com/audreyt/node-webworker-threads) has not been much of a success for this use case. I will have to recheck. Not yet tried with the worker-threads natively available in Node 11.10.
          – Mopparthy Ravindranath
          Nov 11 at 13:03




          1




          1




          I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
          – mihai
          Nov 11 at 13:34




          I haven’t tried worker threads either, I’ll try to do some test myself, but I think you should get better performance if you do a sync read in a worker thead, in theory at least
          – mihai
          Nov 11 at 13:34


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226581%2fcan-nodejs-worker-threads-be-used-for-executing-long-running-file-i-o-based-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values