I want to utilize subprocess Popen
to call strace on Linux.
I also want to catch every line of output strace gives, in realtime if possible.
I came up with the following code for that, but for some reason I can't get it working. I'll only get the output AFTER I terminate the program.
from threading import Thread
from queue import Queue, Empty
pid = 1
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
p = Popen(["strace", "-p", pid], stdout=subprocess.PIPE, bufsize=1)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()
try:
line = q.get_nowait()
print("Got it! "+line)
except Empty:
pass