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
  • 32,027
  • 36
  • 92
  • 151

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
  • 19,323
  • 15
  • 83
  • 135
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
  • 620
  • 7
  • 13