登录
首页 >  Golang >  Go问答

在处理恐慌后继续进行函数执行

来源:stackoverflow

时间:2024-02-24 09:36:18 205浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《在处理恐慌后继续进行函数执行》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我正在尝试创建一个任务调度程序,它可以在给定的时间间隔内执行任务,并且还可以处理发生的恐慌。我的问题是在处理恐慌后如何继续执行该函数。

func scheduleTask(task func() error, interval time.Duration, timeout time.Duration) {
        //add waitgroup and mutex
        var wg sync.WaitGroup
        var mtx sync.Mutex
        var startTime = time.Now()
        //add each task to a goroutine and set interval and timeout to the function
        wg.Add(1)
        go func() {
            defer wg.Done()
            for {
                //check if the time is up
                if time.Since(startTime) > timeout {
                    break
                }
                defer func() {
                    if r := recover(); r != nil {
                        log.Println("Recovering from panic:", r)
                    }
                }()
                
                mtx.Lock()
                task()
                mtx.Unlock()

    
            }
        }()
        wg.Wait()
    }
    
    func main() {
        var a = 0
    
        scheduleTask(func() error {
            time.Sleep(50 * time.Millisecond)
            if a == 3 {
                a++
                panic("oops")
            }
            a++
            return nil
        }, time.Millisecond*100, time.Millisecond*1000)
    
        log.Println(a)
        if a != 10 {
            log.Fatal("Expected it to be 10")
        }
    }

正确答案


恢复时您将退出当前功能。在这种情况下,您可以轻松地将 task() 包装在闭包中。

package main

import (
    "log"
    "sync"
    "time"
)

func scheduleTask(task func() error, interval time.Duration, timeout time.Duration) {
    //add waitgroup and mutex
    var wg sync.WaitGroup
    var mtx sync.Mutex
    var startTime = time.Now()
    //add each task to a goroutine and set interval and timeout to the function
    wg.Add(1)
    go func() {
        defer wg.Done()
        for {
            //check if the time is up
            if time.Since(startTime) > timeout {
                break
            }

            mtx.Lock()
            func() {
                defer func() {
                    if r := recover(); r != nil {
                        log.Println("Recovering from panic:", r)
                    }
                }()

                task()
            }()
            mtx.Unlock()

        }
    }()
    wg.Wait()
}

func main() {
    var a = 0

    scheduleTask(func() error {
        time.Sleep(50 * time.Millisecond)
        if a == 3 {
            a++
            panic("oops")
        }
        a++
        return nil
    }, time.Millisecond*100, time.Millisecond*1000)

    log.Println(a)
    if a != 10 {
        log.Fatal("Expected it to be 10")
    }
}

Playground link

理论要掌握,实操不能落!以上关于《在处理恐慌后继续进行函数执行》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>