import subprocess
import time

process = None

while True:
    print("Starting child...")

    process = subprocess.Popen(
        ["python3.9", "-u", "-m", "app"],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True,
        bufsize=1
    )

    while True:
        line = process.stdout.readline()

        if line == "" and process.poll() is not None:
            break

        if line:
            print(line, end="")
            if "Request timed out" in line or "Transport flood." in line:
                process.kill()
                break

    process.wait()
    time.sleep(1)