我的理解是,如果调用阻塞库,非阻塞 Web 服务器(node.js、eventmachine、tornado)可能会停止运行。 Golang 也是如此吗?如果一个 goroutine 被阻塞,另一个 goroutine 会自动获得 CPU 访问权限,还是必须等待被阻塞的 goroutine ‘yield’?如果是前者,那么库不需要是非阻塞的,不是吗?我问是因为我还没有看到任何明确声明它们是非阻塞的 Redis/Mongo 库。
最佳答案
My understanding is that non-blocking web servers (node.js, eventmachine, tornado) can grind to a halt if they make a call to a blocking library. Is this true for Golang as well?
不,不是。 Go 例程将在 IO 上产生,或者运行时将根据需要创建新的操作系统线程。
If one goroutine gets blocked, is another one automatically given access to the CPU
是的 - go routines 在任何类型的 IO 或 channel 通信上产生。
or do they have to wait for the blocked goroutine to 'yield'?
不,他们没有。
If it is the former, then libraries don't need to be non-blocking, do they? I ask because I haven't seen any Redis/Mongo libraries that explicitly state that they're non-blocking.
没有库(或一般的 Go 代码)不需要是非阻塞的,这使得它们更容易编写和维护。在我看来,这是 Go 的一大优点。运行时会巧妙地运行 1000 多个 go 例程,而您只需编写简单的命令式代码。
关于node.js - Golang - 库需要是非阻塞的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17846505/