4

I want to run a long process (calculix simulation) via python.

As mentioned here one can read the console string with communicate().

As far as I understand the string is returned after the process is completed? Is there a possibility to get the console output while the process is running?

Community
  • 1
  • 1
daniel
  • 34,281
  • 39
  • 104
  • 158

2 Answers2

2

You have to use subprocess.Popen.poll to check process terminates or not.

while sub_process.poll() is None:
    output_line = sub_process.stdout.readline()

This will give you runtime output.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
1

This should work:

sp = subprocess.Popen([your args], stdout=subprocess.PIPE)
while sp.poll() is None: # sp.poll() returns None while subprocess is running
  output = sp.stdout # here you have acccess to the stdout while the process is running
  # Do stuff with stdout

Notice we don't call communicate() on subprocess here.

rparent
  • 630
  • 7
  • 14