You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I basically had something in the lines of following:
// Create a shared bufferconstsharedBuff=newSharedArrayBuffer(1024)constsharedInt32Array=newInt32Array(sharedBuff)// Create a workerconstworker=createWorker(sharedBuff)// Initiate workworker.postMessage('do_work')// block main thread and wait for job to completeatomic.wait(sharedInt32Array,a,b)// print doneconsole.log('complete')
This wasn't so good/effective
cuz it was hard to debug the worker and doing console logs while the main thread was blocked
But i manage to find a solution that worked out okey in the end
instead of using postMessages i just simple transfered the main process.stdout.fd and process.stderr.fd to the worker thread and wrote data directly to that stdio instead of sending postMessages back and forth (which is unnecessary and complicated)
console.log() writes to stdout, which is an asynchrononous operation in Node.js (potentially also on the main thread, but always in worker threads).
You are trying to synchronize an asynchronous operation across threads by blocking one of the threads' event loops. However, when console.log() returns, there is no guarantee that the relevant I/O operations have actually completed, as has been explained in various issues and in the official documentation.
You say that the output "is in complete wrong order," but there is no wrong order. It might not be the order that you expect, but asynchronous operations across threads are not guaranteed to complete in any particular order.
Please feel free to contribute to the various existing discussions on this matter. In particular, #47036 already implements a better variant of your proposal. I will close this issue because it does not add anything that hasn't already been discussed elsewhere.
I basically had something in the lines of following:
and the worker dose something like
What this boils down to is:
which is in complete wrong order. cuz the main thread is blocked.
and the worker do the work and console log before the main thread can continue
it should have been
My suspicion was the you send postMessages back to the main thread and then forward it to the main process.std
and i was right...
node/lib/internal/worker/io.js
Lines 360 to 396 in 0b3fcfc
node/lib/internal/main/worker_thread.js
Lines 189 to 194 in 0b3fcfc
This wasn't so good/effective
cuz it was hard to debug the worker and doing console logs while the main thread was blocked
But i manage to find a solution that worked out okey in the end
instead of using postMessages i just simple transfered the main
process.stdout.fd
andprocess.stderr.fd
to the worker thread and wrote data directly to that stdio instead of sending postMessages back and forth (which is unnecessary and complicated)So here is kind of my workaround:
The logs will be in the correct order
and it dose not need any complicated postMessage protocol like
So my proposal is that you write directly to the main stdio file descriptors instead,
The text was updated successfully, but these errors were encountered: