uasyncio example study

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
koufdell
Posts: 4
Joined: Fri Oct 11, 2019 9:17 pm

uasyncio example study

Post by koufdell » Fri Oct 11, 2019 9:23 pm

peace everyone
new in the world of micropython , i came across uasyncio and searched for a basic example
after some tries i have some questions:
what's the meaning of runleq as argument of uasyncio.get_event_loop() ?
is this example valid?
using uasyncio 2.0 on a esp 01
import utime
import uasyncio

def is_prime(x):
return not any(x//i == x/i for i in range(x-1,2,-1))

async def highest_prime_below(x):
print('Highest prime below %d' %x)
for y in range(x-1,0,-1):
if is_prime(y):
print('highest prime below %d is %d' % (x,y))
return y
#await uasyncio.sleep_ms(1)
yield
return None

async def finish():

t0 = utime.ticks_ms()
while len(loop.runq)!=0:
await uasyncio.sleep_ms(1)
t1 = utime.ticks_ms()
print('done in {:2f} secs'.format((t1-t0)/1000))
loop.stop()
loop.close()

loop = uasyncio.get_event_loop()
loop.create_task(highest_prime_below(20000))
loop.create_task(highest_prime_below(30000))
loop.create_task(highest_prime_below(100))
loop.create_task(highest_prime_below(37562))
loop.create_task(finish())
loop.run_forever()



thx everyone..

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

Re: uasyncio example study

Post by pythoncoder » Sat Oct 12, 2019 6:31 am

I advise against polling runq. You can initialise the event loop to use larger run and time queues. This is only necessary in applications with a large number of concurrent tasks. In most applications you initialise the event loop with

Code: Select all

loop = uasyncio.get_event_loop()
and then ignore the run and time queues: these are internal to uasyncio and are not part of its API.

You will find a tutorial on uasyncio and numerous example scripts in this repository.
Peter Hinch
Index to my micropython libraries.

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

Re: uasyncio example study

Post by pythoncoder » Sat Oct 12, 2019 7:09 am

To add to my comments, your is_prime() function does an equality test between a float with an integer. This is not good practice because of imprecision in the way floats are represented as machine words. You should use the modulus operator:

Code: Select all

def is_prime(x):
    return all(x % i for i in range(x-1, 1, -1))
Also, to ensure that division by 2 is tested, the range should be as above. Otherwise 4 will be reported as prime.

I'm sure you're aware that though this is simple it is not an efficient way of testing primality.
Peter Hinch
Index to my micropython libraries.

koufdell
Posts: 4
Joined: Fri Oct 11, 2019 9:17 pm

Re: uasyncio example study

Post by koufdell » Sat Oct 12, 2019 1:07 pm

thank you for your time sir
is it better to define a loop.run_until_complete() handler or use the logic i came up with(watching the queue and stopping the loop.run_forever())
if so what handler should i write for loop.run_until_complete()

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

Re: uasyncio example study

Post by pythoncoder » Sun Oct 13, 2019 6:48 am

As stated above I don't think you should watch the queue*. There are better ways of testing whether coroutines have run to completion which don't require knowledge of uasyncio internals. The simplest is to have each set a global boolean before they terminate, with the main coroutine looping (with a delay) until all flags were set. More Pythonic would be to create a class: have the coroutines as bound methods and have the flags as bound variables.

My preferred solution would be to use the synchronisation primitives which may be found in this repo. A Barrier instance can cause a coroutine to pause until all members of a set of other coroutines have run to completion.

*Watching the run queue would not actually work if your coroutines have nonzero delays because of the relationship between runq and timeq.
Peter Hinch
Index to my micropython libraries.

koufdell
Posts: 4
Joined: Fri Oct 11, 2019 9:17 pm

Re: uasyncio example study

Post by koufdell » Sun Oct 13, 2019 10:57 am

thx sir for your time again much appreciated

Post Reply