From ed0260a8842fdb6c7ba749f114c9e66e5f2f7040 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Wed, 9 Oct 2019 23:19:41 +0800 Subject: [PATCH] glasgow: use asyncio for writing to stdout --- glasgow-applet.diff | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/glasgow-applet.diff b/glasgow-applet.diff index c01729b..00c24ac 100644 --- a/glasgow-applet.diff +++ b/glasgow-applet.diff @@ -10,12 +10,13 @@ index 35b8960..3f37b9f 100644 +from .logic import LogicApplet diff --git a/software/glasgow/applet/logic.py b/software/glasgow/applet/logic.py new file mode 100644 -index 0000000..529d4e1 +index 0000000..c721acd --- /dev/null +++ b/software/glasgow/applet/logic.py -@@ -0,0 +1,77 @@ +@@ -0,0 +1,92 @@ +import sys +import logging ++import os +import asyncio +from nmigen.compat import * +from nmigen.compat.genlib.cdc import MultiReg @@ -52,6 +53,19 @@ index 0000000..529d4e1 + ) + + ++async def async_stdout(limit=asyncio.streams._DEFAULT_LIMIT, loop=None): ++ if loop is None: ++ loop = asyncio.get_event_loop() ++ ++ writer_transport, writer_protocol = await loop.connect_write_pipe( ++ lambda: asyncio.streams.FlowControlMixin(loop=loop), ++ os.fdopen(sys.stdout.fileno(), "wb")) ++ writer = asyncio.streams.StreamWriter( ++ writer_transport, writer_protocol, None, loop) ++ ++ return writer ++ ++ +class LogicApplet(GlasgowApplet, name="logic"): + logger = logging.getLogger(__name__) + preview = True @@ -81,9 +95,10 @@ index 0000000..529d4e1 + pass + + async def interact(self, device, args, iface): ++ output_stream = await async_stdout() + while True: + data = await iface.read(65536) -+ sys.stdout.buffer.write(data) ++ output_stream.write(data) + +# ------------------------------------------------------------------------------------------------ +