为什么 nodejs fs sync 同步 apis 在应该在工作池上执行时阻塞主线程(偶数循环)(nodejs 的默认值)?

Why nodejs fs sync synchronous apis block the main thread (even loop) when it should be execute on the worker pool (default of nodejs)?

这是一道理论题,

我之前认为在nodejs中昂贵的任务如I/O, Network,...将在worker池中的worker上执行,而不是主节点事件循环,所以它不会阻塞事件循环。

我看了一些关于它的文章,e.g nodejs docs

但是今天我看了一篇文章,上面写着:

"As async is definitely a better solution for such cases, it's the sync versions of the methods that have proper -Sync suffix. You should keep in mind that the use of sync methods is just highly not-recommended and, when used, will block the main thread from performing any other operations. Just use them with care (and only if you really have to)!"

那么,如果这是真的,为什么这些同步任务在应该在工作池上执行时会阻塞主偶数循环?

谢谢

So if it's true, why these synchronous tasks will block the main even loop when it should be execute on the worker pool?

因为同步版本是专门设计来阻塞主线程的,所以它会在返回之前等待它们完成。它们旨在阻止 API。这就是他们的目的。如果他们不这样做,他们将是非阻塞和异步的,而不是阻塞和同步的。

问题不在于实际工作是在哪个线程上完成的。问题在于同步版本专门设计用于让主线程等待直到它们完成。这是为了在某些需要阻塞行为的情况下简化编程。例如,在服务器的启动代码中使用一些同步文件 I/O 并不少见。例如,require() 阻止文件 I/O 加载脚本。

但是,正如您在本文中所见,您只想在服务器启动后 运行 和处理请求后使用异步文件 I/O。