Code: Select all
import subprocess
import os
import pty
master, slave = pty.openpty()
proc = subprocess.Popen(["micropython"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=slave,
universal_newlines=True)
stdin = os.fdopen(master, 'w')
def keep_reading(stream):
while True:
data = stream.read(1)
if data:
print(data, end="")
else:
print("done")
break
import threading
threading.Thread(target=keep_reading, args=[proc.stdout]).start()
from time import sleep
while True:
# allow stdout reader to complete
# TODO: should be dome properly with select.select
sleep(0.1)
cmd = input()
if not cmd:
break
stdin.write(cmd + "\n")
stdin.flush()