Can nodejs worker threads be used for executing long running file I/O based javascript code?
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
node.js multithreading
edited Nov 11 at 13:08
asked Nov 9 at 13:25
Mopparthy Ravindranath
93921634
93921634
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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