V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
helloworld12
V2EX  ›  Go 编程语言

golang 协程调度 帮忙翻译理解下

  •  
  •   helloworld12 · 2018-10-24 19:09:32 +08:00 · 2303 次点击
    这是一个创建于 1983 天前的主题,其中的信息可能已经有所发展或是发生改变。

    if you run any tight loops that are not making function calls, you will cause latencies within the scheduler and garbage collection

    是说,如果你循环中没有进行函数调用,那么调度或者垃圾回收时,会有延时 ?

    看了上下文,说当函数调用的时候,有协程调度的埋点,这样的话,如果循环中没有函数调用,就不会有调度的延时啊?

    It ’ s critically important that function calls happen within reasonable timeframes.

    是说,如果函数调用频度要合理,不要过多,也不要过少吗?

    5 条回复    2018-10-25 00:27:53 +08:00
    xiaxiaocao
        1
    xiaxiaocao  
       2018-10-24 19:23:56 +08:00   ❤️ 1
    没有埋点一个协程就会一直占了 P 啊,P 队列上其他的协程就没机会执行了
    Leigg
        2
    Leigg  
       2018-10-24 19:50:36 +08:00 via iPhone   ❤️ 1
    gc 本就是针对变量,函数一类对象的,gc 是有开销的,不会很频繁的执行,字面意思你没翻译错。
    TheCure
        3
    TheCure  
       2018-10-24 19:58:24 +08:00   ❤️ 1
    调度器发现一个 routine 运行了很久需要暂停的时候, 会给这个 routine 设置一个 flag. 在 function call 的时候, 会去检查这个 flag, 然后决定是否暂停运行.
    像 java 里的 GC Pause, 也需要执行到 safepoint 的时候,停止运行, 虽然 go 用的是协程, 但是和这个 GC Pause 也类似
    TheCure
        4
    TheCure  
       2018-10-24 19:59:43 +08:00   ❤️ 1
    所以给的建议是, 如果很蠢的写了一个 for 循环, 里面都是 i++这种没有发生函数调用, 也不可能阻塞的代码, 这个 routine 会一直跑下去.
    reus
        5
    reus  
       2018-10-25 00:27:53 +08:00
    https://github.com/golang/proposal/blob/master/design/24543-non-cooperative-preemption.md

    Proposal: Non-cooperative goroutine preemption

    很可能 1.12 就会实现,所以探讨这些没什么意义,这些知识很快就会过时。
    goroutine 不是协程。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2740 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:34 · PVG 20:34 · LAX 05:34 · JFK 08:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.