Use 2 python script below.
The .py wizard will use Popen to start a new process and start a watcher thread that will kill the process after 3.0 seconds.
The slave must call the flush method if there is no new line in the data written to stdout (a flash is also called in the windows '\n' ).
Be careful, the time module is not a high-precision timer.
The process loading time in extreme cases can exceed 3.0 seconds (reading the executable file from a flash drive with USB 1.0)
Master.py
import subprocess, threading, time def watcher(proc, delay): time.sleep(delay) proc.kill() proc = subprocess.Popen('python Slave.py', stdout = subprocess.PIPE) threading.Thread(target = watcher, args = (proc, 3.0)).start() data = bytearray() while proc: chunk = proc.stdout.read(1) if not chunk: break data.extend(chunk) print(data)
Slave.py
import time, sys while True: time.sleep(0.1) sys.stdout.write('aaaa') sys.stdout.flush()
source share