duanguan5922 2013-07-24 23:24
浏览 18
已采纳

Golang-库是否需要非阻塞?

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? If one goroutine gets blocked, is another one automatically given access to the CPU, 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.

  • 写回答

1条回答 默认 最新

  • dox90448 2013-07-24 23:34
    关注

    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?

    No it isn't. Go routines will yield on IO or the runtime will create new OS threads as necessary.

    If one goroutine gets blocked, is another one automatically given access to the CPU

    Yes it is - go routines yield on any sort of IO or channel communication.

    or do they have to wait for the blocked goroutine to 'yield'?

    No they don't.

    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.

    No libraries (or Go code in general) don't need to be non blocking which makes them much easier to write and maintain. This is a major plus point of Go in my opinion. The runtime does the clever bit running 1000s of go routines and you just write simple imperative code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料