Python Forum
Flask read real time print from subprocess without deadlock
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask read real time print from subprocess without deadlock
#1
Hello. Maybe someone could help? The problem is with subprocess.
I need to run the subprocess (generator.py) and show the output real-time on HMTL via Flask . As this subprocess starts, Flask switches to deadlock. And all the generated information is obtained only at the end of the subprocess. It's clear that everything works on localhost on Windows, but not on Linux. Maybe someone knows how to get around that deadlock? See example below.

generator.py
import random
from time import sleep
for i in range(0, 20, 1):
    sleep(0.5)
    print(random.randint(0, 100))
app.py
@app.route('/interactive')
def interactive():
    def read():
        proc = subprocess.Popen(
            ["python3", "users/ID4/generator.py"], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
        for line in iter(proc.stdout.readline, ""):
            yield line
    return Response(read(), mimetype='text/html')


@app.route('/stream123')
def stream123():
    def generate():
        process = subprocess.Popen("ping -c 5 google.com", stdout=subprocess.PIPE, shell=True)
        while True:
            line = process.stdout.readline().rstrip()
            if not line:
                break
            yield line
    return Response(generate(), mimetype='text/html')
Larz60+ write Jun-02-2021, 10:26 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode on future posts.
buran write Jun-02-2021, 10:05 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask, read H5 text value tantony 4 1,391 Oct-31-2022, 05:29 PM
Last Post: tantony
  Flask app cannot read js and css files. Aggam 0 1,600 Nov-04-2020, 12:33 PM
Last Post: Aggam
  Read owl file using python flask Gayathri 1 2,394 Nov-20-2019, 12:56 PM
Last Post: ChislaineWijdeven
  Read Save RadioButtons from Database in Python Flask Webpage Gary8877 0 7,110 Apr-11-2019, 12:33 AM
Last Post: Gary8877
  live(real time) database retrive x64 1 3,391 Dec-06-2016, 06:43 PM
Last Post: nilamo
  Read input file and print hyperlinks Emmanouil 8 15,052 Oct-23-2016, 07:26 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020