How to check if a function is running in worker thread in node?

87
January 26, 2021, at 05:00 AM

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?

Answer 1

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.

READ ALSO
PyQt5 - setting background color and background image

PyQt5 - setting background color and background image

I wonder how do I set background color AND background image in Python GUI app using PyQt5I can't figure out how to set them simultaneously

105
How to take column from two table in whereExists clause in laravel 5.5

How to take column from two table in whereExists clause in laravel 5.5

I have three table 1)allcasefileofuserrequest 2)allscanfileofuserrequest 3)userrequestforcasecopies

85
Recursive select does not return date list [closed]

Recursive select does not return date list [closed]

Want to improve this question? Add details and clarify the problem by editing this post

99
Cannot publish new release to Google Play because of Sensitive app permissions

Cannot publish new release to Google Play because of Sensitive app permissions

The problem I'm facing is that I cannot create any new releases on any of the Google Play release tracks (internal, alpha, beta, production) since one of the old releases required location permissons

73