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

Issue

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.

Solution

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.

Answered By – ZAky

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.