duanpan7011 2015-06-29 16:36
浏览 76
已采纳

转:类似于“ tail -f”的生成器

I had this convenient function in Python:

def follow(path):
    with open(self.path) as lines:
        lines.seek(0, 2)  # seek to EOF

        while True:
            line = lines.readline()
            if not line:
                time.sleep(0.1)
                    continue
                yield line 

It does something similar to UNIX tail -f: you get last lines of a file as they come. It's convenient because you can get the generator without blocking and pass it to another function.

Then I had to do the same thing in Go. I'm new to this language, so I'm not sure whether what I did is idiomatic/correct enough for Go.

Here is the code:

func Follow(fileName string) chan string {

    out_chan := make(chan string)

    file, err := os.Open(fileName)
    if err != nil {
        log.Fatal(err)
    }

    file.Seek(0, os.SEEK_END)
    bf := bufio.NewReader(file)

    go func() {
        for {
            line, _, _ := bf.ReadLine()

            if len(line) == 0 {
                time.Sleep(10 * time.Millisecond)
            } else {
                out_chan <- string(line)
            }
        }

        defer file.Close()
        close(out_chan)
    }()

    return out_chan
}

Is there any cleaner way to do this in Go? I have a feeling that using an asynchronous call for such a thing is an overkill, and it really bothers me.

  • 写回答

2条回答 默认 最新

  • dqypcghd381390 2015-06-29 17:47
    关注

    I suggest creating a wrapper around a reader that sleeps on EOF:

    type tailReader struct {
        io.ReadCloser
    }
    
    func (t tailReader) Read(b []byte) (int, error) {
        for {
            n, err := t.ReadCloser.Read(b)
            if n > 0 {
                return n, nil
            } else if err != io.EOF {
                return n, err
            }
            time.Sleep(10 * time.Millisecond)
        }
    }
    
    func newTailReader(fileName string) (tailReader, error) {
        f, err := os.Open(fileName)
        if err != nil {
            return tailReader{}, err
        }
    
        if _, err := f.Seek(0, 2); err != nil {
            return tailReader{}, err
        }
        return tailReader{f}, nil
    }
    

    This reader can be used anywhere an io.Reader can be used. Here's how loop over lines using bufio.Scanner:

    t, err := newTailReader("somefile")
    if err != nil {
        log.Fatal(err)
    }
    defer t.Close()
    scanner := bufio.NewScanner(t)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        fmt.Fprintln(os.Stderr, "reading:", err)
    }
    

    The reader can also be used to loop over JSON values appended to the file:

    t, err := newTailReader("somefile")
    if err != nil {
        log.Fatal(err)
    }
    defer t.Close()
    dec := json.NewDecoder(t)
    for {
        var v SomeType
        if err := dec.Decode(&v); err != nil {
           log.Fatal(err)
        }
        fmt.Println("the value is ", v)
    }
    

    There are a couple of advantages this approach has over the goroutine approach outlined in the question. The first is that shutdown is easy. Just close the file. There's no need to signal the goroutine that it should exit. The second advantage is that many packages work with io.Reader.

    The sleep time can be adjusted up or down to meet specific needs. Decrease the time for lower latency and increase the time to reduce CPU use. A sleep of 100ms is probably fast enough for data that's displayed to humans.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频