Here is an example code of a slow application. (Imagine the boot of a Linux) This is the DUT, which should be controlled.
#linux.py
import time
print('Loading kernel ......')
time.sleep(0.5)
print('Loading foo [ OK ]')
time.sleep(0.5)
print('Loading bar [ OK ]')
time.sleep(0.5)
input('login> ')
I want to control via pexpect python script like the following.
# controller.py
import pexpect
import sys
pybin = sys.executable
command = pybin + ' linux.py'
p = pexpect.spawn(command)
p.expect('> ', timeout = 5)
print(p.before.decode(), end='')
print(p.match.group(0).decode())
p.sendline('')
It is OK, but I cannot get the console's output of the "Linux.py's" boot before total boot-up. I mean, I didn't get feedback before the login prompt. Imagine, there is an error during the boot-up. The script above will fail with timeout exception.
My GOAL
To monitor the child process and print it's output while waiting to prompt. How can it be done?