英文:
How to stop the time when not stepping through lines during debugging?
问题
当调试一个使用context.WithTimeout
的程序时,如果你不逐行调试,时钟会继续走动,所以在你能够调试依赖于给定上下文的代码片段之前,该上下文就已经完成了,因此你感兴趣的调试代码片段不会执行。例如,在下面的代码片段中,我必须增加timestamp
的值才能够逐行调试do()
和retry()
,否则超时会在我能够调试之前就触发:
package main
import (
"context"
"fmt"
"math/rand"
"time"
)
const success = 0.1
const timeframe = time.Microsecond * 2
func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeframe)
do(ctx, cancel)
}
func do(ctx context.Context, cancel context.CancelFunc) {
defer retry(ctx, cancel)
select {
case <-ctx.Done(): // 在我到达这行代码之前就会触发超时
panic("fail")
default:
if rand.Float64() < success {
cancel()
fmt.Println("success")
return
} else {
fmt.Println("fail")
}
}
}
func retry(ctx context.Context, cancel context.CancelFunc) {
r := recover()
if r == nil {
do(ctx, cancel)
}
}
我在编程和技术方面没有太多使用英语的经验,所以请随意要求重新表达。
英文:
When debugging a program that makes use of say context.WithTimeout
when you are not stepping through lines the clock keeps ticking, so before you can debug code piece that depends on given context, that context becomes done thus the code piece you are interested in debugging does not execute. For example in the following snippet I have to increase the timestamp
value to be able to step through do()
and retry()
because otherwise timeout will be reached way before I can do it:
package main
import (
"context"
"fmt"
"math/rand"
"time"
)
const success = 0.1
const timeframe = time.Microsecond * 2
func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeframe)
do(ctx, cancel)
}
func do(ctx context.Context, cancel context.CancelFunc) {
defer retry(ctx, cancel)
select {
case <-ctx.Done(): // timeout will be reached before i reach this line
panic("fail")
default:
if rand.Float64() < success {
cancel()
fmt.Println("success")
return
} else {
fmt.Println("fail")
}
}
func retry(ctx context.Context, cancel context.CancelFunc) {
r := recover()
if r == nil {
do(ctx, cancel)
}
}
I haven't used English that much to talk about programming and tech so, feel free to ask to rephrase.
答案1
得分: 2
在调试过程中,如果不逐行执行代码,无法停止时间流逝。你只能通过增加超时时间来进行手动调试,或者不使用调试器。
英文:
> How to stop the time when not stepping through lines during debugging?
You simply cannot.
You either have to increase your timeouts so large that you can do your manual debugging or not use a debugger.
答案2
得分: 1
你可以使用在这个答案中解释的构建标签技巧。
基本上创建两个文件,一个用于保存正常的超时值,另一个用于在使用delve时保存较长的超时值。
// +build delve
package main
import "time"
const Timeframe = 10 * time.Hour
// +build !delve
package main
import "time"
const Timeframe = 2 * time.Microsecond
在调用delve时使用--build-flags='-tags=delve'
来选择正确的文件。
英文:
You can use the build tag trick explained in this answer.
Basically create 2 files, one to hold the normal timeout value and the other to hold a longer timeout when running under delve.
// +build delve
package main
import "time"
const Timeframe = 10 * time.Hour
// +build !delve
package main
import "time"
const Timeframe = 2 * time.Microsecond
Use --build-flags='-tags=delve'
when calling delve to choose the correct file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论