英文:
How do I check if a goroutine is created successfully?
问题
在Go语言中,go
是用来创建一个goroutine的关键字。然而,它似乎不返回任何值,那么我如何检测goroutine是否成功创建呢?
Go语言使用以下方式来检查函数是否成功:
value, err = myFunc(value)
对于go
关键字是否有类似的用法来检测创建错误呢?如果创建失败,似乎go
会抛出运行时异常。
我想进行一次测试,找出我可以为一个CPU创建的最大goroutine数量。
英文:
In Go, go
is the keyword to spawn a goroutine. However, it seems the keyword doesn't return any value, so how could I detect if the goroutine is spawned correctly?
It seems Go uses the following ways to check whether a func is successful or not.
value, err = myFunc(value)
Is there a similar usage for the go
keyword to detect a creation error? It seems go
will throw a runtime exception if it failed.
I want to make a test to find out the maximum number of goroutine I could create for a CPU.
答案1
得分: 3
你已经知道:
value, err = myFunc(value)
这是处理异常的惯用方式,通过返回内置的error
类型。在某种程度上,你可以将其与已检查的异常进行比较。不过,在你的情况下,无法生成新的goroutine
更像是运行时异常。Go语言处理这些异常的方式是使用panic。你可以在代码中使用内置的recover()
函数来处理它们,该函数将尝试重新控制执行流程。如果没有这样做,panic
将沿着调用栈向上传递,直到导致程序崩溃。
请注意,recover()
必须在一个被defer
的函数中调用,这些函数会被推入一个列表,并且总是在其所在的函数末尾调用 - 所以即使发生panic
,它们也会被调用,从而允许你调用recover()
。如果你只是尝试在函数末尾调用recover()
(或者在发生panic的子函数之后的任何地方),执行永远不会到达它。如果你可以处理panic(recover()
不返回err
),以便程序实际上可以继续执行,它将从抛出panic的函数的位置开始执行。
我认为上面的博文已经足够了,但如果你需要更多示例,请在这里留言。
此外,你的系统很可能受到RAM内存的限制,而不是CPU。
英文:
As you already know:
value, err = myFunc(value)
is the idiomatic way to handle exceptions by returning the built-in error
type. In a way you can compare it to a checked exception, I guess. In your case though, failing to spawn a new goroutine
is more of a runtime exception. How golang handles those is by using panics. You can handle them in your code with the built-in recover()
function, which will try to regain control of the execution flow. Without that the panic
will go up the stack until it crashes the program.
Notice that recover()
has to be called in a function which is being defer
ed, those functions are pushed into a list and are always called at the end of the function in which they were defered - so even when the panic
occurs they will be called, allowing you to call recover()
. If you just try to call recover()
at the end of your function (or well anywhere after you panicking subfunction) the execution will never reach it. If you can handle the panic (recover()
doesn't return an err
) so that your program can actually continue it will execute from the point where the function that threw the panic was.
Think the above blog post is enough but if you need more examples just comment here.
Also your system will most probably be bounded by RAM memory rather than CPU.
答案2
得分: 1
一个 goroutine 的创建(或多或少)只是一个内存分配。通常情况下,你无法捕获内存分配异常,goroutine 也是如此。
如果你的程序耗尽了内存,通常除了退出并重新启动之外,你无法做任何事情。
英文:
A goroutine creation is (more or less) just a memory allocation. You cannot in general catch memory allocation exceptions and it's the same with goroutines.
If your program runs out of memory, there's usually nothing you can do about it beyond quitting and restarting.
答案3
得分: 0
如果你正在使用一个返回错误的go routine函数,你可以改为在一个匿名函数上调用go,并在匿名函数中处理错误。
go func() {
value, err := myFunc()
}()
英文:
If the function you are using a go routine that returns an error, you can instead call go on an anonymous function and handle the error in the anonymous function.
go func() {
value, err := myFunc()
}()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论