Page 1 of 1

uasyncio lock not working

Posted: Wed Feb 17, 2021 12:20 pm
by goldimman
Hi

I am running on: MicroPython v1.14-56-g5cb91afb9 on 2021-02-17; PYBD-SF6W with STM32F767IIK

and the below prints False. Any ideas how to get this working?

import uasyncio
l = uasyncio.Lock()
l.acquire()
print("locked ", l.locked())

Device path COM10
Quit: Ctrl+] | Stop program: Ctrl+C | Reset: Ctrl+D
Type 'help()' (without the quotes) then press ENTER.
locked False
MicroPython v1.14-56-g5cb91afb9 on 2021-02-17; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>>
>>>
>>>

Re: uasyncio lock not working

Posted: Thu Feb 18, 2021 12:35 am
by jimmo
goldimman wrote:
Wed Feb 17, 2021 12:20 pm
and the below prints False. Any ideas how to get this working?
You need to use lock within an asyncio-based program.

In this case, the acquire method on Lock is a coroutine, i.e. you need to "await" it.

So:

Code: Select all

import uasyncio as asyncio

lock = asyncio.Lock()

async def my_task():
  await lock.acquire()
  print("locked", lock.locked())

asyncio.run(my_task())
If you call a coroutine without awaiting it, the code will not run.

Re: uasyncio lock not working

Posted: Thu Feb 18, 2021 8:24 am
by pythoncoder
See example code here.