node.js - Golang - 库需要是非阻塞的吗?

标签 node.js go tornado eventmachine

我的理解是,如果调用阻塞库,非阻塞 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/

相关文章:

node.js - 解码 mongodump 的 stderr

python - 使用await或yield迭代循环会导致错误

python - 我如何使用 Tornado 提供(永无止境的)系统调用

shell - Golang exec.Command 在退出代码不为零时返回 nil 错误

go - 使用括号导入多个包会出错吗?

go - 方法与函数使用 golang

python - 使用 Flask ping 远程 PC,导致服务器阻塞

node.js - 如何在node js的函数内使用multer文件上传

mysql - Node.JS:使用 alasql 将数据从 mysql 导出到 excel

node.js - gcloud 模块加载缓慢