|
|
|
|
|
by fdkz
5275 days ago
|
|
I'm using the mkfifo method on linux/macosx: 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"
|
|