asyncio.StreamReader similar behaviour to uart.read() possible? #12996
-
Hi everyone, I try to pipe RS323 signals over UART to a client program via websocket. Microdot provides the Websocket and I have passed the data to it in the past by reading directly from uart.read(): @app.route('/echo')
@with_websocket
async def echo(request, ws):
while True:
await ws.send(passthrough(uart.read())) (passthrough is a function that turns Nones into To allow for users to send commands as well, I was told that I need to make the whole process asynchronous . I looked around and found this tutorial which uses asyncio.StreamReader. My slimmed down version of it looks like this: async def receiver():
sreader = asyncio.StreamReader(uart)
while True:
res = await sreader.read(1)
if res is None:
return b''
else:
return res
@app.route('/echo')
@with_websocket
async def echo(request, ws):
while True:
await ws.send(await receiver()) and it works, I can see the output visualized by xterm.js, but there is one problem: Can i tweak StreamReader in any way? Am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
@JohannesNeu do you need to use |
Beta Was this translation helpful? Give feedback.
-
@peterhinch I have increased the value to something big as you suggested. (2048 to be precise)
Could I run into a situation where only half of the buffer is filled and it would keep on waiting for more data? |
Beta Was this translation helpful? Give feedback.
sreader.read(2048)
will return under two conditions:It won't block under any circumstances.