首页 > 后端开发 > Golang > 正文

Go Playground 和本地机器之间 Goroutine 的行为有何不同?

Mary-Kate Olsen
发布: 2024-10-23 17:42:24
原创
451 人浏览过

How Do Goroutines Behave Differently Between Go Playground and Local Machine?

Go Playground 和本地机器上的 Go 之间的差异

问题:Go Playground 中的 Goroutines 与本地机器

为了澄清对 Goroutines 的误解,此代码在 Go Playground 中运行:

<code class="go">package main

import (
    "fmt"
)

func other(done chan bool) {
    done <- true
    go func() {
        for {
            fmt.Println("Here")
        }
    }()
}

func main() {
    fmt.Println("Hello, playground")
    done := make(chan bool)
    go other(done)
    <-done
    fmt.Println("Finished.")
}</code>
登录后复制

在 Go Playground 中,它导致错误:“进程花费太长时间。”这表明在其他 Goroutine 中创建的 Goroutine 无限期地运行。

但是,在本地运行相同的代码会产生立即输出:

<code class="go">Hello, playground.
Finished.</code>
登录后复制

这意味着当主 Goroutine 时,其他 Goroutine 退出

说明

差异是由于 GOMAXPROCS 的默认值造成的。

在 Go Playground 上, GOMAXPROCS 设置为 1。意味着一次只能运行一个 goroutine。当 other 中创建的 Goroutine 不阻塞时(例如,通过在通道上等待),调度程序将不会切换到其他 Goroutine。

由于主 Goroutine 在 did 通道上阻塞,因此调度程序会切换到其他内部的 goroutine。然后,other 中的 goroutine 启动另一个 goroutine,并无限循环。由于 GOMAXPROCS 为 1,主 Goroutine 不再继续,死循环继续运行,导致超时。

在本机上, GOMAXPROCS 一般默认为 CPU 核数(例如,4 或 8)。这允许多个 goroutine 并发运行。当主 Goroutine 在完成通道上阻塞时,调度程序会切换到另一个 Goroutine。这可能是其他 Goroutine 中的 Goroutine,也可能是运行无限循环的 Goroutine。

由于主 Goroutine 最终会完成,因此无限循环将不再运行。因此,程序将正常终止,而无需等待无限循环完成。

结论

在 Go Playground 中运行 goroutine 时,考虑 GOMAXPROCS 的默认值非常重要。要模拟多goroutine并发,请显式将GOMAXPROCS设置为更高的值,例如runtime.GOMAXPROCS(2)。在本地执行中,GOMAXPROCS 的默认设置通常允许预期的并发行为。

以上是Go Playground 和本地机器之间 Goroutine 的行为有何不同?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!