2

I am trying to use worker_threads in node to run some expensive calculation without blocking the main thread.

Is it possible to check if the function is actually running in worker thread, just like using the performance tab in browser devtool?

3
  • Why would the function care? Commented Jan 25, 2021 at 2:44
  • It is important to not block the main thread. The function doesn't care but I do. Commented Jan 25, 2021 at 3:37
  • So your code just wouldn't call the function in the main thread? Commented Jan 25, 2021 at 10:21

2 Answers 2

2

If the worker thread is running blocking code which is presumably why you pushed it to a worker thread in the first place, then it is blocked and it cannot communicate with you directly since it would have to receive a message from you and send a response for you to communicate with it, but if it's blocked doing something else, then it isn't processing messages so you can't talk to it until it's done.

Things you could do:

Have worker thread tell you when it started and when its done

When the function starts running, it sends a message to the main thread. The main thread gets that message and stores that state. When the function completes, it sends a message to the main thread that is has completed. The main thread will receive that and can update the state.

So, as long as the worker thread follows that logic reliably, then at any time, the main thread can just consult the state it has to know whether the worker thread last told it was running or done.

Test message processing responsiveness of the worker thread

Another option would be to send a message to the worker thread with some sort of uniqueID in it and note the time you sent the message. Then, the worker thread (when it is free to process that message) will respond to it and return back the same uniqueID. From the main thread, you can assume that if you don't get a quick response from the message you sent to the worker, then it must have been busy. You have to use the uniqueID because the response will come back sometime in the future and you have to know which responses to pay attention to and which might be old. This is basically just a test to see whether the worker thread is responding to messages quickly - thus indicating that it's not being blocked by some blocking operation that is currently running.

Sign up to request clarification or add additional context in comments.

Comments

1

Found this while looking for the same answer:

const { isMainThread } = require('node:worker_threads'); if (isMainThread) { // this is running in the main thread. } else { // this is running in a worker. } 

If you want to make sure expensive code is not called from the main thread you could do an early return or throw.

function expensive() { if (isMainThread) { throw new Error('Code is too expensive to run on the main thread.'); } else { // run expensive code } } 

Reference: https://nodejs.org/docs/latest-v18.x/api/worker_threads.html#workerismainthread

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.