python3 script as subprocess in python2, empty PIPE

89 views Asked by At

i made this two script to illustrate my problem.

I want a python2 script to have a python3 script as as subprocess with a non blocking read.

Lets say both scripts are in the /tmp folder

The child python3 script is this:

#!/usr/bin/env python3
import time
while True:
    print ("test")
    time.sleep(0.5)

And the parent script is:

#!/usr/bin/env python2
import fcntl
import os
import subprocess
import time

sub = subprocess.Popen(['python3','/tmp/child.py'], cwd='/tmp', bufsize=1, stdout=subprocess.PIPE)

fcntl.fcntl(sub.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

while True:
    try:
        print sub.stdout.read() #Thats the not working part
    except IOError:
        pass
    time.sleep(0.5)

The parent script does not get the output from the child process, and i get no output to stdout either. So the output gets redirected, but its basically lost. This is not intended. How do i get the output from the child process ? In my real script the output from the child script gets processed in the parent script instead of printed.

thank you for help.

0

There are 0 answers