uasyncio: number of tasks in the event loop

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
Posts: 69
Joined: Fri Dec 18, 2015 7:36 pm

uasyncio: number of tasks in the event loop

Post by Jim.S » Sun Apr 15, 2018 12:00 pm

Is there a simple way to get the number of tasks queued in the event loop?

I think I have written a program that adds a new task every time one task is run, and need to check my understanding of what is happening.

User avatar
Posts: 2479
Joined: Fri Jul 18, 2014 8:01 am
Location: UK

Re: uasyncio: number of tasks in the event loop

Post by pythoncoder » Mon Apr 16, 2018 7:49 am

The uasyncio V2.0 EventLoop class maintains two queues: runq and waitq. The former holds tasks which are ready to run, while the latter holds ones which are waiting pending later execution. In general you can get the total number of tasks with

Code: Select all

loop = asyncio.get_event_loop()
ntasks = len(loop.runq) + len(loop.waitq)
However if you're using the methods which schedule callback functions rather than coroutines (e.g. call_soon) the length of runq can be overstated because the args to the callback are also placed on the queue. So each callback which is ready for execution but hasn't yet been run will occupy two entries.

If you're just using coroutines I think you'll be OK.
Peter Hinch

Post Reply