设为首页 加入收藏

TOP

初识go的tomb包(二)
2018-10-19 15:50:37 】 浏览:109
Tags:初识 tomb
rror, or the Kill or Killf method is called by any goroutine in the system (tracked or not), the tomb Err is set, Alive is set to false, and the Dying channel is closed to flag that all tracked goroutines are supposed to willingly terminate as soon as possible.

Once all tracked goroutines terminate, the Dead channel is closed, and Wait unblocks and returns the first non-nil error presented to the tomb via a result or an explicit Kill or Killf method call, or nil if there were no errors.

It is okay to create further goroutines via the Go method while the tomb is in a dying state. The final dead state is only reached once all tracked goroutines terminate, at which point calling the Go method again will cause a runtime panic.

Tracked functions and methods that are still running while the tomb is in dying state may choose to return ErrDying as their error value. This preserves the well established non-nil error convention, but is understood by the tomb as a clean termination. The Err and Wait methods will still return nil if all observed errors were either nil or ErrDying.

 

关于gopkg.in/tomb.v1使用例子

golang官网上看到了这样一个例子,觉得用的挺好的就放这里

package main

import (
    "gopkg.in/tomb.v1"
    "log"
    "sync"
    "time"
)

type foo struct {
    tomb tomb.Tomb
    wg   sync.WaitGroup
}

func (f *foo) task(id int) {
    for i := 0; i < 10; i++ {
        select {
        case <-time.After(1e9):
            log.Printf("task %d tick\n", id)
        case <-f.tomb.Dying():
            log.Printf("task %d stopping\n", id)
            f.wg.Done()
            return
        }
    }
}

func (f *foo) Run() {
    f.wg.Add(10)
    for i := 0; i < 10; i++ {
        go f.task(i)
    }
    go func() {
        f.wg.Wait()
        f.tomb.Done()
    }()
}

func (f *foo) Stop() error {
    f.tomb.Kill(nil)
    return f.tomb.Wait()
}

func main() {
    var f foo
    f.Run()
    time.Sleep(3.5e9)
    log.Printf("calling stop\n")
    f.Stop()
    log.Printf("all done\n")
}

在关于tomb这个包的说明上,说的也非常清楚,tomb包用于追踪一个goroutine的声明周期,如:as alive,dying or dead and the reason for its death

关于v1 版本官网的说明

The tomb package offers a conventional API for clean goroutine termination.

A Tomb tracks the lifecycle of a goroutine as alive, dying or dead, and the reason for its death.

The zero value of a Tomb assumes that a goroutine is about to be created or already alive. Once Kill or Killf is called with an argument that informs the reason for death, the goroutine is in a dying state and is expected to terminate soon. Right before the goroutine function or method returns, Done must be called to inform that the goroutine is indeed dead and about to stop running.

A Tomb exposes Dying and Dead channels. These channels are closed when the Tomb state changes in the respective way. They enable explicit blocking until the state changes, and also to selectively unblock select statements accordingly.

When the tomb state changes to dying and there's still logic going on within the goroutine, nested functions and methods may choose to return ErrDying as their error value, as this error won't alter the tomb state if provided to the Kill method. This is a convenient way to follow standard Go practices in the context of a dying tomb..

 

小结

可以从上面的文章以及使用例子上看出,tomb包是一个非常实用的一个包,后面会继续整理一下关于tomb v1版本的源码,看看人家是如何实现的,学习学习

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇go 源码学习之---Tail 源码分析 下一篇go开源项目influxdb-relay源码分..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目