【问题标题】:How to track the execution flow (the functions being called)如何跟踪执行流程(被调用的函数)
【发布时间】:2019-12-02 07:30:16
【问题描述】:

我曾使用 PHP 和 Python 等语言,但最近我有一个 Go 项目需要维护,该项目利用了多个工作程序和 goroutine。

在 PHP 或 Python 中,我们可以将代码的执行视为所有同步调用。但是我发现很难理解 Go 中的代码执行是如何发生的,尤其是在 worker 和 goroutine 中。任何人都可以建议我使用任何工具来获取执行流程(被调用的函数的名称)吗?

例如,如果有一个API,其中有各种goroutine,并且在这些goroutine内部,会调用其他goroutine,那么如何知道这些函数被调用的顺序。

【问题讨论】:

标签: go concurrency execution goroutine


【解决方案1】:

Go 运行时跟踪工具会帮助你进行跟踪,但你需要在 go code start 中添加跟踪选项

package main

import (
    "os"
    "runtime/trace"
)

func main() {
    trace.Start(os.Stderr) //start the trace 
    defer trace.Stop() // defer to the end
    .... //rest of the code
}

然后使用 ,

go run main.go 2> trace.out  
then use the trace tool
go tool trace trace.out.

此链接有更多信息https://blog.gopheracademy.com/advent-2017/go-execution-tracer/

【讨论】:

  • 在标准错误中打印并不是很理想。如果在 stderr 中出现另一个错误或打印任何内容怎么办。直接将其写入文件会更好。
【解决方案2】:

[I]如果有一个 API 中有各种 goroutines,并且在这些 goroutines 中,会调用其他 goroutines,[...]

没有。

【讨论】:

  • 也许这一点信息需要更多解释...... ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
相关资源
最近更新 更多