2

I have a flutter project called zed, my goal is to monitor the output of flutter run, as long as pressing r, the output will increase.

enter image description here

To automatically implement this workflow, my implementation is

import subprocess

bash_commands = f'''
cd ../zed
flutter run  --device-id web-server --web-hostname 192.168.191.6 --web-port 8352
'''

process = subprocess.Popen('/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=False)
output, err= process.communicate(bash_commands.encode('utf-8'))
print(output, err)
output, _ = process.communicate('r'.encode('utf-8'))
print(output)

It's not working as I expected, there is nothing printed on the screen.

7
  • Does flutter have the same output if it isn't outputting to a terminal emulator? Commented Aug 24, 2022 at 13:19
  • @SargeATM yes, when I redirect the output to a file with flutter run --device-id web-server --web-hostname 192.168.191.6 --web-port 8352 > log.txt, there is right content in the file Commented Aug 24, 2022 at 13:23
  • I would try using process.stdin and process.stdout instead of process.communicate and see if there is a difference. Commented Aug 24, 2022 at 13:36
  • @SargeATM I tried with process.stdin.write(bash_commands.encode('utf-8')) print(process.stdout.readline()), there is no difference. Commented Aug 24, 2022 at 13:40
  • Sanity test by sending bash_commands = "echo give me a break please\n". I'm on my windows machine right now so all I can do is throw out ideas of what I would try. Commented Aug 24, 2022 at 13:46

1 Answer 1

2

Use process.stdin.write() instead of process.communicate()

process.stdin.write(bash_commands)
process.stdin.flush()

But why you ask

Popen.communicate(input=None, timeout=None) Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate

communicate(...) doesn't return until the pipe is closed which typically happens when the subprocess closes. Great for ls -l not so good for long running subprocesses.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.