forked from peterhinch/micropython-async
-
Notifications
You must be signed in to change notification settings - Fork 1
/
roundrobin.py
40 lines (31 loc) · 916 Bytes
/
roundrobin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# roundrobin.py Test/demo of round-robin scheduling
# Author: Peter Hinch
# Copyright Peter Hinch 2017 Released under the MIT license
# Result on Pyboard with print('Foo', n) commented out
# executions/second:
# Using yield: 3187
# Using sleep_ms(0) 2238
# Note using yield in a coro is "unofficial" and may not
# work in future uasyncio revisions.
try:
import asyncio_priority as asyncio
except ImportError:
import uasyncio as asyncio
count = 0
period = 5
async def foo(n):
global count
while True:
# yield
await asyncio.sleep_ms(0)
count += 1
print('Foo', n)
async def main(delay):
print('Testing for {} seconds'.format(period))
await asyncio.sleep(delay)
loop = asyncio.get_event_loop()
loop.create_task(foo(1))
loop.create_task(foo(2))
loop.create_task(foo(3))
loop.run_until_complete(main(period))
print('Coro executions per sec =', count/period)