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
Worker threads in Node.js send their standard IO to the main thread for processing. When synchronous operations are implemented, the blocking of these operations can cause the logs of these worker threads to be synchronized and output only after the computation results are completed.
Similarly, following the implementation of whatwg-workers, by rewriting the write methods of stdout and stderr streams within worker threads, it is possible to allow the standard input and standard error within the worker threads to be directly written into the streams.
// caused by https://github.com/nodejs/node/blob/0b3fcfcf351fba9f29234976eeec4afb09ae2cc0/lib/internal/worker/io.js#L360// override process write to make stdout and stderr work in worker threadfunctionoverrideProcess4WorkThread(){for(const[i,stream]of[process.stdout,process.stderr].entries()){// process.stdout.fd === 1 & process.stderr.fd === 2 . In Worker threads, this field does not exist.constfd=i+1conststreamOriginWritev=stream._writev?.bind(stream)stream._writev=(chunks,cb)=>{if(chunks.length===0){return}constchunk=chunks.pop()!// type-coverage:ignore-next-line -- we can't controlfs.write(fd,chunk.chunkasstring,null,chunk.encoding,err=>{if(err)cb(err)elseif(chunks.length===0)cb()elsestreamOriginWritev?.(chunks,cb)})}}}
and test demo output
If allowed, I'm glad to raise a pr
The text was updated successfully, but these errors were encountered:
According to the research based on the issue at jimmywarting/await-sync#1 (comment)
Worker threads in
Node.js
send their standard IO to the main thread for processing. When synchronous operations are implemented, the blocking of these operations can cause the logs of these worker threads to be synchronized and output only after the computation results are completed.Similarly, following the implementation of
whatwg-workers
, by rewriting the write methods ofstdout
andstderr
streams within worker threads, it is possible to allow the standard input and standard error within the worker threads to be directly written into the streams.and test demo output
If allowed, I'm glad to raise a pr
The text was updated successfully, but these errors were encountered: