两个进程实时从/向同一个文件进行读写

huangapple go评论117阅读模式
英文:

Two process read & write from/to the same file in real time

问题

我有一个使用情况,其中一个运行Python的进程将其执行日志写入文件。另一个运行在Go语言中的进程希望实时读取文件的内容,例如日志流。但是为了读取文件的内容,似乎我必须等到Python进程完成。有没有办法让Python进程正常终止,并在最后生成日志文件的同时将日志流传输给Go语言进程?

我的目的是将Python进程的日志流传输给Go语言进程。

英文:

I have a use case where one process, running python, write its execution logs to a file. Another process running in Goilang, wants to read the content of the file in real time, e.g. log streaming. But in order to read the content of the file, it seems I have to wait until the Python process is complete. Is there a way to let the python process terminate normally with the log file generated in the end and also get the log streaming to the golang process?

My purpose is to get the python process log stream to the golang process.

答案1

得分: 0

1. 简单的方法

如果你正在使用Linux,可以将Python的日志写入标准输出(stdout),然后使用管道(pipe)。
sourse.py | target (用Go语言编写)

package main

import (
    "bufio"
    "fmt"
    "os"
)

/*
 Three ways of taking input
   1. fmt.Scanln(&input)
   2. reader.ReadString()
   3. scanner.Scan()

   Here we recommend using bufio.NewScanner
*/

func main() {
    // 创建动态数组
    arr := make([]string, 0)
    scanner := bufio.NewScanner(os.Stdin)
    for {
        fmt.Print("输入文本: ")
        // 从标准输入中扫描一行
        scanner.Scan()
        // 获取扫描到的字符串
        text := scanner.Text()
        if len(text) != 0 {
            fmt.Println(text)
            arr = append(arr, text)
        } else {
            break
        }
    }
    // 使用收集到的输入
    fmt.Println(arr)
}

用法:

echo "what a wanderful world" | ./go-bin

还可以阅读这个Python重定向到标准输出的问题。

2. 正确的方法

对于长时间运行的进程,使用命名管道(Named pipe)可能更好。它是一个Linux文件(FIFO) GNU pipe

Python将数据写入该文件,然后Golang读取它
Go语言中的FIFO示例

3. 可能有点过度的方法

编写一个Golang Web服务器,并从Python调用该服务器的端点。

如果可以更改Python源代码。

在这种解决方案中,安全性也应该更加注意。

英文:

1. The simple

If you are using Linux write log from Python to stdout and use pipe.
sourse.py | target (written in go)

package main

import (
"bufio"
"fmt"
"os"
)

/*
 Three ways of taking input
   1. fmt.Scanln(&input)
   2. reader.ReadString()
   3. scanner.Scan()

   Here we recommend using bufio.NewScanner
*/

func main() {
// To create dynamic array
arr := make([]string, 0)
scanner := bufio.NewScanner(os.Stdin)
for {
    fmt.Print("Enter Text: ")
    // Scans a line from Stdin(Console)
    scanner.Scan()
    // Holds the string that scanned
    text := scanner.Text()
    if len(text) != 0 {
        fmt.Println(text)
        arr = append(arr, text)
    } else {
        break
    }

}
// Use collected inputs
fmt.Println(arr)
}

Usage:

echo "what a wanderful world" |./go-bin 

Also read this Python redirect to StdOut

2. The right.

It may be better for a long running process to use Named pipe.
Which is a linux file (FIFO) GNU pipe.

Python write to this file and Golang read it
FIFO example in go

3. The probably oveverkill.

Write a Golang web server and call the server endpoint from python.

If you can change the Python source.

Security also should get more attention with this sulotion.

huangapple
  • 本文由 发表于 2022年10月7日 11:43:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/73982134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定