Skip to content

asyncio_priority  #16

Closed
Closed
@tvo321

Description

@tvo321

In trying to use a StreamReader (w/ uart) and a lower priority coro. It appears StreamReader's readline() never yields (the way it is implemented from examples I found).

Here's an example of what I'm referring to:

import asyncio_priority as asyncio
from pyb import UART
uart = UART(1, 115200)

async def reader():
    sreader = asyncio.StreamReader(uart)
    while True:
        res = await sreader.readline()
        print(res)

async def heartbeat(led):
    while True:
        led.toggle()
        await asyncio.after_ms(500)  # Will hang while a coro loops on a zero delay

loop = asyncio.get_event_loop()
loop.create_task(heartbeat(pyb.LED(4)))
loop.create_task(reader())
loop.run_forever()

I think a workaround would be to use the following (but is there a workaround to use StreamReader with your low priority coro library?):

async def reader():
    while True:
        if uart.any()> THRESHOLD: #check enough bytes has been received to ensure a '\n' 
            res = uart.readline() 
        await asyncio.sleep_ms(20)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions