forked from goodboy/tractor
1
0
Fork 0

Play around with relaying each char to child

stin_char_relay
Tyler Goodlet 2020-07-23 20:36:41 -04:00
parent 7b5af3b2d4
commit 042f2326db
1 changed files with 36 additions and 23 deletions

View File

@ -4,6 +4,7 @@ Multi-core debugging for da peeps!
import pdb import pdb
import sys import sys
import tty import tty
import termios
from functools import partial from functools import partial
from typing import Awaitable, Tuple from typing import Awaitable, Tuple
@ -40,38 +41,50 @@ async def _hijack_stdin_relay_to_child(
actor = tractor.current_actor() actor = tractor.current_actor()
proc = subactoruid2proc(actor, subactor_uid) proc = subactoruid2proc(actor, subactor_uid)
# nlb = [] nlb = []
line = []
async def hijack_stdin(): async def hijack_stdin():
log.info(f"Hijacking stdin from {actor.uid}") log.info(f"Hijacking stdin from {actor.uid}")
# try: try:
# # disable cooked mode # disable cooked mode
# fd = sys.stdin.fileno() fd = sys.stdin.fileno()
# old = tty.tcgetattr(fd) old = tty.tcgetattr(fd)
# tty.setcbreak(fd) # tty.setcbreak(fd)
tty.setcbreak(fd)
# trap std in and relay to subproc # trap std in and relay to subproc
async_stdin = trio.wrap_file(sys.stdin) async_stdin = trio.wrap_file(sys.stdin)
async with aclosing(async_stdin): async with aclosing(async_stdin):
# while True: while True:
async for msg in async_stdin: # async for msg in async_stdin:
log.trace(f"Stdin input:\n{msg}") msg = await async_stdin.read(1)
# nlb.append(msg) nlb.append(msg)
# encode to bytes # encode to bytes
bmsg = str.encode(msg) bmsg = str.encode(msg)
log.trace(f"Stdin str input:\n{msg}")
log.trace(f"Stdin bytes input:\n{bmsg}")
# relay bytes to subproc over pipe # relay bytes to subproc over pipe
await proc.stdin.send_all(bmsg) await proc.stdin.send_all(bmsg)
# termios.tcflush(fd, termios.TCIFLUSH)
# line = str.encode(''.join(nlb)) # don't write control chars to local stdout
# print(line) if bmsg not in (b'\t'):
# mirror input to stdout
sys.stdout.write(msg)
sys.stdout.flush()
if bmsg in _pdb_exit_patterns: if bmsg == b'\n':
log.info("Closing stdin hijack") line = str.encode(''.join(nlb))
break # print(line.decode())
# finally: if line in _pdb_exit_patterns:
# tty.tcsetattr(fd, tty.TCSAFLUSH, old) log.info("Closing stdin hijack")
line = []
break
finally:
tty.tcsetattr(fd, tty.TCSAFLUSH, old)
# schedule hijacking in root scope # schedule hijacking in root scope
actor._root_nursery.start_soon(hijack_stdin) actor._root_nursery.start_soon(hijack_stdin)