简体   繁体   中英

Execute external *.exe application using Python and display output in real time

Let me introduce the goal of the application I'm building: I am creating a front-end GUI using PySide (Qt) for a fortran based application used in the framework of CFD. The fortran application is compiled as a *.exe file, and, when executed, it continuously provides the simulated lapse of time and other output details (when I launch it from the console, these data continously appear until it finishes).

For example, if I executed the external code from the console I would get

>> myCFDapplication.exe
   Initializing...
   Simulation start...
   Time is 0.2
   Time is 0.4
   Time is 0.6
   Time is 0.8
   Time is 1.0
   Simulation finished
>>

With quite a long lapse of time between "Time is .." and the next line.

The objective of the GUI is to generate the initialization files for the external application, to launch the external application and finally to provide the user the computation output information in real time (as plane text). From other similar topics in this site, I have been able to launch my external application from Python using the following code

import os, sys
import subprocess

procExe = subprocess.Popen("pru", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)

while procExe.poll() is None:
line = procExe.stdout.readline()
print("Print:" + line)

but the output is only displayed when the execution finishes, and moreover, the whole GUI freezes until that moment.

I would like to know how to launch my external application using Python, getting the output in real time and passing it to the GUI instantaneously, if possible. The idea would be to print the output in different lines inside a "TextEdit" dialog using the function "append(each_output_line)".

Check out Non-blocking read on a subprocess.PIPE in python and look at the use of Queues to do a non-blocking read of the subprocess. The biggest change for your Qt application is that you are probably going to have to use multiprocessing since, as you have observed, anything blocking in your application is going to freeze the GUI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM