uasyncio --- Calling async functions sequentially

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
doceave
Posts: 31
Joined: Fri Feb 14, 2020 4:02 pm

uasyncio --- Calling async functions sequentially

Post by doceave » Wed Mar 18, 2020 8:44 am

Hi there everyone

Micropython and uasyncio fresh installations on an ESP32 generic.

I have number of async functions that interact with hardware. On startup I would like to run a few of these functions one at a time (each completing before the next is run). Between some of these functions I would further like execution of code to wait for user confirmation of hardware functionality, bmo button press, before proceeding.

At present all functions are asynchronous and simply calling each function results in these running simultaneously. Inserting delays are also not practical given widely variable amounts of time for some of the functions to execute fully.

Kindly assist me with an example of this sequential calling of functions, allowing each to execute fully before calling of the next.

Thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Calling async functions sequentially

Post by pythoncoder » Wed Mar 18, 2020 1:25 pm

If coroutine A awaits execution of coroutine B, execution of A will suspend until B is complete

Code: Select all

import uasyncio as asyncio

async def foo():
    await asyncio.sleep(1)  # Real function would interrogate hardware

async def bar():
    print('started')
    await foo()
    print('got here')  # a second later
    await foo()
    print('Now I got here')
Peter Hinch
Index to my micropython libraries.

doceave
Posts: 31
Joined: Fri Feb 14, 2020 4:02 pm

Re: uasyncio --- Calling async functions sequentially

Post by doceave » Wed Mar 18, 2020 2:44 pm

Thanks Peter

My setup is precisely like this at present.

The problem being that function A and function B should not be linked in any way as I may need to call each independently during operation of the machine.

Real world application:
I have around 10 functions, each representing a piece of hardware. During initial startup of the device I run through a startup test sequence which serves to allow the user to check that hardware is working. E.g. read_temp_left_coil, heater_left, optical_detect_left, pressure_check_left, run_pump_left.

I simply want to call each function, allow it to execute fully, then call the next +- return a value --- much like regular function (rather than coros).

Key here is that these functions are unrelated and one function cannot be responsible for calling the next as the context of the initial call may change.

Thanks --- uasyncio is totally amazing!

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio --- Calling async functions sequentially

Post by pythoncoder » Thu Mar 19, 2020 7:44 am

I'm sorry but I still don't understand the problem. Assume we have several coros - I'll just write two but there could be any number

Code: Select all

async def test_heater():
    await some_hardware()
    return await get_temperature()  # Value from a async code

async def test_motor():
    start_motor()
    await asyncio.sleep(10)  # allow to get to speed
    return read_rpm()  # Value from synchronous code
Those asynchronous functions may be run sequentially or independently exactly like synchronous functions, except that they can only be run in a coroutine. Sequential running:

Code: Select all

async def full_test():
    results = {}
    results['heater'] = await test_heater()
    results['motor'] = await test_motor()
    results['coffee_machine'] = await test_coffee()
or they can be run individually in a different context

Code: Select all

async def run_motor(speed):
    rpm = await test_motor()
    if 10 < rpm < 20:
    	await set_motor_speed(speed)
    else:
        await halt_and_catch_fire()
uasyncio is indeed amazing. Damien is close to releasing a complete rewrite which will fix some bugs and limitations which have languished unfixed for a long time. I assume you've seen my async repo which includes various resources for the current version.
Peter Hinch
Index to my micropython libraries.

Post Reply