import os, sys, time, subprocess, fcntl
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
read_fd, write_fd = os.pipe()
fcntl.fcntl(read_fd, fcntl.F_SETFL, os.O_NONBLOCK) # don't know of any windows equivalent for this line
proc = subprocess.Popen("ping www.google.com", cwd=sys.path[0], stdout=write_fd, stderr=write_fd, shell=True)
while 1:
try:
s = os.read(read_fd, 1024)
if s:
sys.stdout.write(s)
except OSError:
pass
time.sleep(0.1) import os
import sys
import time
import subprocess
# turn off stdout buffering. otherwise we won't see things like wget progress-bars that update without newlines.
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
pipename = "tempfile"
if os.path.exists(pipename):
os.remove(pipename)
# create a pipe. one side is connected to the ping process, other side is connected to python.
os.mkfifo(pipename)
read_fd = os.open(pipename, os.O_RDONLY|os.O_NONBLOCK)
writer = open(pipename, "w+")
proc = subprocess.Popen("ping www.google.com", cwd=sys.path[0], stdout=writer, stderr=writer, shell=True)
while 1:
try:
# nonblocking poll data from the external process.
s = os.read(read_fd, 1024)
if s:
sys.stdout.write(s)
except OSError:
pass
# sidenote: minimum sleep time is 1/64 seconds on many windows pc-s.
time.sleep(0.1)
# remember to remove the pipe "tempfile"
And more technical information: https://www.safetyresearch.net/Library/BarrSlides_FINAL_SCRU...