doushajian2018 2018-09-21 23:01
浏览 14
已采纳

睡眠过程,直到在Go中完成

I am trying to automate a process in Go. I have been able to implement threads and do the process accordingly however the output is mixed and matched.

I was wondering if there is a way to show the output as it is produced by the program and according to the program's process. So if task A completes before task B, we show A's output before B, or vice-versa.

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "sync"
)

var url string
var wg sync.WaitGroup

func nikto() {
    cmd := exec.Command("nikto", "-h", url)
    cmd.Stdout = os.Stdout
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    wg.Done()
}

func whois() {

    cmd := exec.Command("whois", "google.co")
    cmd.Stdout = os.Stdout
    err := cmd.Run()
    if err != nil {
        log.Fatal(err)
    }
    wg.Done()
}
func main() {
    fmt.Printf("Please input URL")
    fmt.Scanln(&url)
    wg.Add(1)
    go nikto()
    wg.Add(1)
    go whois()
    wg.Wait()
}
  • 写回答

1条回答 默认 最新

  • drtiwd06558 2018-09-21 23:43
    关注

    In your process, you pass the os.Stdout file descriptor directly to the commands you invoke to run your child processes. This means the STDOUT pipe of the child processes will be connected directly to your Go program's standard output, and will likely be interleaved if both child processes write simultaneously.


    The simplest way to fix this requires you to buffer the output from the STDOUT pipe of the child process in your Go program, so you can intercept the output and control when it is printed.

    The Cmd type in the os/exec package provides a function call Output() which will invoke the child process and return the contents of STDOUT in a byte slice. Your code can be adapted with ease to implement this pattern and process the results, for example:

    func whois() {
        cmd := exec.Command("whois", "google.co")
        out, err := cmd.Output()
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(out)
        wg.Done()
    }
    

    Interleaving of output

    If you use functions in the fmt package to print output, there is no guarantee that concurrent calls to fmt.Println will not be interleaved.

    To prevent interleaving, you may choose to serialize access to STDOUT, or use a logger which is safe for concurrent use (such as the log package). Here is an example of serializing access to STDOUT in the Go process:

    package main
    
    import (
        "fmt"
        "log"
        "os/exec"
        "sync"
    )
    
    var url string
    
    func nikto(outChan chan<- []byte) {
        cmd := exec.Command("nikto", "-h", url)
        bs, err := cmd.Output()
        if err != nil {
            log.Fatal(err)
        }
        outChan <- bs
    }
    
    func whois(outChan chan<- []byte) {
        cmd := exec.Command("whois", "google.com")
        bs, err := cmd.Output()
        if err != nil {
            log.Fatal(err)
        }
        outChan <- bs
    }
    
    func main() {
        outChan := make(chan []byte)
    
        fmt.Printf("Please input URL")
        fmt.Scanln(&url)
        go nikto(outChan)
        go whois(outChan)
    
        for i := 0; i < 2; i++ {
            bs := <-outChan
            fmt.Println(string(bs))
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式