uasyncio --- Calling one async function from another

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: 22
Joined: Fri Feb 14, 2020 4:02 pm

uasyncio --- Calling one async function from another

Post by doceave » Sun Feb 23, 2020 8:09 am

Hi there everyone

Another NOOB question relating to the uasyncio way of doing things:

In the section of code below, if I wanted to call one routine from another. The routine will execute then return a value.

I am clearly doing something incorrectly. Again I hope an expert will point out the error with a short explanation as to how to do this properly.

Code: Select all

async def adc_right():
    while True:
            ### Do things
            ### Do things
            ### Do things
            await asyncio.sleep_ms(8)
            ### Do things
            ### Do things
            ### Do things
            if index_put_R == BUFFERSIZE:
            	### How do I call the function write_ADC_log_to_file???...
            	### And get a value back from this function???

async def write_ADC_log_to_file():
            global index_put_R
            with open('ADC_log_R.csv', 'w') as ADC_log_R:
                for line in range(0, index_put_R):
                    ADC_log_R.write(str(line) + "/" + str(data_ch0_R[line]) + "/" + str(data_ch1_R[line]) + '\n')
            ### Do things
            ### return (value)

async def heater_control():
    while True:
            ### Do things
            await asyncio.sleep_ms(1000)
            ### Do things

loop = asyncio.get_event_loop()
t1 = loop.create_task(adc_right())
t2 = loop.create_task(heater_control())
loop.run_forever()

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

Re: uasyncio --- Calling one async function from another

Post by pythoncoder » Sun Feb 23, 2020 10:54 am

This is covered in my tutorial.

If you have a coro defined with async def, there are two things you can do with it. You can (in another coro) await its completion.

Code: Select all

async def my_coro(x):
    await asyncio.sleep(1)
    return 2*x

async def foo():
    result = await my_coro(5)  # Will pause for 1 second
    print(result)
Or you can set it running as an independent task with loop.create_task(my_coro()).

Code: Select all

async def my_coro(x):
    await asyncio.sleep(1)
    return 2*x

async def foo():
    loop = asyncio.get_event_loop()
    loop.create_task(my_coro(5))  # Immediate return (result will be lost)
    asyncio.sleep(0)
The create_task call is synchronous and returns immediately. Because it is synchronous it can be issued from synchronous code or from a coro.
Peter Hinch

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

Re: uasyncio --- Calling one async function from another

Post by doceave » Sun Feb 23, 2020 3:49 pm

This is a much simpler illustration than presented in your tutorial :)

Many thanks indeed. The former examples does exactly what I need.

Post Reply