Python uasyncio.run() Examples

The following are 30 code examples of uasyncio.run(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module uasyncio , or try the search function .
Example #1
Source File: asyntest.py    From micropython-samples with MIT License 6 votes vote down vote up
def ack_test():
    printexp('''message was set
message_wait 1 got message with value 0
message_wait 2 got message with value 0
Cleared ack1
Cleared ack2
Cleared message
message was set
message_wait 1 got message with value 1
message_wait 2 got message with value 1

... text omitted ...

message_wait 1 got message with value 5
message_wait 2 got message with value 5
Cleared ack1
Cleared ack2
Cleared message
I've seen attack ships burn on the shoulder of Orion...
Time to die...
''', 10)
    asyncio.create_task(run_ack())
    asyncio.run(ack_coro(6))

# ************ Test Lock and Message classes ************ 
Example #2
Source File: delay_test.py    From micropython-async with MIT License 6 votes vote down vote up
def isr_test():  # Test trigger from hard ISR
    from pyb import Timer
    s = '''
Timer holds off cb for 5 secs
cb should now run
cb callback
Done
'''
    printexp(s, 6)
    def cb(v):
        print('cb', v)

    d = Delay_ms(cb, ('callback',))

    def timer_cb(_):
        d.trigger(200)
    tim = Timer(1, freq=10, callback=timer_cb)

    print('Timer holds off cb for 5 secs')
    await asyncio.sleep(5)
    tim.deinit()
    print('cb should now run')
    await asyncio.sleep(1)
    print('Done') 
Example #3
Source File: switches.py    From micropython-async with MIT License 6 votes vote down vote up
def test_sw():
    s = '''
close pulses green
open pulses red
'''
    print('Test of switch scheduling coroutines.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register coros to launch on contact close and open
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    run()

# Test for the switch class with a callback 
Example #4
Source File: switches.py    From micropython-async with MIT License 6 votes vote down vote up
def test_swcb():
    s = '''
close toggles red
open toggles green
'''
    print('Test of switch executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(toggle, (red,))
    sw.open_func(toggle, (green,))
    run()

# Test for the Pushbutton class (coroutines)
# Pass True to test suppress 
Example #5
Source File: switches.py    From micropython-async with MIT License 6 votes vote down vote up
def test_btncb():
    s = '''
press toggles red
release toggles green
double click toggles yellow
long press toggles blue
'''
    print('Test of pushbutton executing callbacks.')
    print(helptext)
    print(s)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    yellow = LED(3)
    blue = LED(4)
    pb = Pushbutton(pin)
    pb.press_func(toggle, (red,))
    pb.release_func(toggle, (green,))
    pb.double_func(toggle, (yellow,))
    pb.long_func(toggle, (blue,))
    run() 
Example #6
Source File: asyntest.py    From micropython-samples with MIT License 6 votes vote down vote up
def condition_test():
    printexp('''cond02 0 Awaiting notification.
cond02 1 Awaiting notification.
cond02 2 Awaiting notification.
cond02 3 Awaiting notification.
cond02 4 Awaiting notification.
cond02 5 Awaiting notification.
cond02 6 Awaiting notification.
cond02 5 triggered. tim = 1
cond02 6 triggered. tim = 1
cond02 3 triggered. tim = 3
cond02 4 triggered. tim = 3
cond02 1 triggered. tim = 5
cond02 2 triggered. tim = 5
cond02 0 triggered. tim = 7
cond04 99 Awaiting notification and predicate.
cond04 99 triggered. tim = 9
Done.
''', 13)
    asyncio.run(cond_go())

# ************ Queue test ************ 
Example #7
Source File: asyntest.py    From micropython-async with MIT License 6 votes vote down vote up
def ack_test():
    printexp('''message was set
message_wait 1 got message with value 0
message_wait 2 got message with value 0
Cleared ack1
Cleared ack2
Cleared message
message was set
message_wait 1 got message with value 1
message_wait 2 got message with value 1

... text omitted ...

message_wait 1 got message with value 5
message_wait 2 got message with value 5
Cleared ack1
Cleared ack2
Cleared message
I've seen attack ships burn on the shoulder of Orion...
Time to die...
''', 10)
    asyncio.create_task(run_ack())
    asyncio.run(ack_coro(6))

# ************ Test Lock and Message classes ************ 
Example #8
Source File: asyntest.py    From micropython-async with MIT License 6 votes vote down vote up
def msg_test():
    printexp('''Test Lock class
Test Message class
waiting for message
run_lock 1 waiting for lock
run_lock 1 acquired lock
run_lock 2 waiting for lock
run_lock 3 waiting for lock
Waiting 5 secs before setting message
run_lock 1 released lock
run_lock 2 acquired lock
run_lock 2 released lock
run_lock 3 acquired lock
run_lock 3 released lock
message was set
got message
Message status OK
Tasks complete
''', 5)
    asyncio.run(run_message_test())

# ************ Barrier test ************ 
Example #9
Source File: asyntest.py    From micropython-samples with MIT License 6 votes vote down vote up
def msg_test():
    printexp('''Test Lock class
Test Message class
waiting for message
run_lock 1 waiting for lock
run_lock 1 acquired lock
run_lock 2 waiting for lock
run_lock 3 waiting for lock
Waiting 5 secs before setting message
run_lock 1 released lock
run_lock 2 acquired lock
run_lock 2 released lock
run_lock 3 acquired lock
run_lock 3 released lock
message was set
got message
Message status OK
Tasks complete
''', 5)
    asyncio.run(run_message_test())

# ************ Barrier test ************ 
Example #10
Source File: asyntest.py    From micropython-async with MIT License 6 votes vote down vote up
def barrier_test1():
    printexp('''Running (runtime = 5s):
report instance 0 waiting
report instance 1 waiting
report instance 2 waiting
report instance 2 done
report instance 1 done
report instance 0 done
my_coro running
my_coro was cancelled.

Exact report instance done sequence may vary, but 3 instances should report
done before my_coro runs.
''', 5)
    asyncio.run(bart())

# ************ Semaphore test ************ 
Example #11
Source File: asyntest.py    From micropython-async with MIT License 6 votes vote down vote up
def condition_test():
    printexp('''cond02 0 Awaiting notification.
cond02 1 Awaiting notification.
cond02 2 Awaiting notification.
cond02 3 Awaiting notification.
cond02 4 Awaiting notification.
cond02 5 Awaiting notification.
cond02 6 Awaiting notification.
cond02 5 triggered. tim = 1
cond02 6 triggered. tim = 1
cond02 3 triggered. tim = 3
cond02 4 triggered. tim = 3
cond02 1 triggered. tim = 5
cond02 2 triggered. tim = 5
cond02 0 triggered. tim = 7
cond04 99 Awaiting notification and predicate.
cond04 99 triggered. tim = 9
Done.
''', 13)
    asyncio.run(cond_go())

# ************ Queue test ************ 
Example #12
Source File: delay_test.py    From micropython-async with MIT License 6 votes vote down vote up
def ctor_test():  # Constructor arg
    s = '''
Trigger 5 sec delay
Retrigger 5 sec delay
Callback should run
cb callback
Done
'''
    printexp(s, 12)
    def cb(v):
        print('cb', v)

    d = Delay_ms(cb, ('callback',), duration=5000)

    print('Trigger 5 sec delay')
    d.trigger()
    await asyncio.sleep(4)
    print('Retrigger 5 sec delay')
    d.trigger()
    await asyncio.sleep(4)
    print('Callback should run')
    await asyncio.sleep(2)
    print('Done') 
Example #13
Source File: as_rwGPS_time.py    From micropython-async with MIT License 5 votes vote down vote up
def drift(minutes=5):
    try:
        asyncio.run(do_drift(minutes))
    finally:
        asyncio.run(shutdown())

# ******** Time printing demo ********
# Every 10s print the difference between GPS time and RTC time 
Example #14
Source File: delay_test.py    From micropython-async with MIT License 5 votes vote down vote up
def reduce_test():  # Test reducing a running delay
    s = '''
Trigger 5 sec delay
Callback should run
cb callback
Callback should run
Done
'''
    printexp(s, 11)
    def cb(v):
        print('cb', v)

    d = Delay_ms(cb, ('callback',))

    print('Trigger 5 sec delay')
    d.trigger(5000)  # Test extending time
    await asyncio.sleep(4)
    print('Callback should run')
    await asyncio.sleep(2)
    d.trigger(10000)
    await asyncio.sleep(1)
    d.trigger(3000)
    await asyncio.sleep(2)
    print('Callback should run')
    await asyncio.sleep(2)
    print('Done') 
Example #15
Source File: delay_test.py    From micropython-async with MIT License 5 votes vote down vote up
def launch_test():
    s = '''
Trigger 5 sec delay
Coroutine should run: run to completion.
Coroutine starts
Coroutine ends
Coroutine should run: test cancellation.
Coroutine starts
Coroutine should run: test awaiting.
Coroutine starts
Coroutine ends
Done
'''
    printexp(s, 20)
    async def cb(v, ms):
        print(v, 'starts')
        await asyncio.sleep_ms(ms)
        print(v, 'ends')

    d = Delay_ms(cb, ('coroutine', 1000))

    print('Trigger 5 sec delay')
    d.trigger(5000)  # Test extending time
    await asyncio.sleep(4)
    print('Coroutine should run: run to completion.')
    await asyncio.sleep(3)
    d = Delay_ms(cb, ('coroutine', 3000))
    d.trigger(5000)
    await asyncio.sleep(4)
    print('Coroutine should run: test cancellation.')
    await asyncio.sleep(2)
    coro = d.rvalue()
    coro.cancel()
    d.trigger(5000)
    await asyncio.sleep(4)
    print('Coroutine should run: test awaiting.')
    await asyncio.sleep(2)
    coro = d.rvalue()
    await coro
    print('Done') 
Example #16
Source File: adctest.py    From micropython-async with MIT License 5 votes vote down vote up
def test():
    try:
        asyncio.run(adctest())
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()
        print()
        print(st) 
Example #17
Source File: as_rwGPS_time.py    From micropython-async with MIT License 5 votes vote down vote up
def usec(minutes=1):
    try:
        asyncio.run(do_usec(minutes))
    finally:
        asyncio.run(shutdown()) 
Example #18
Source File: astests.py    From micropython-async with MIT License 5 votes vote down vote up
def run_tests():
    asyncio.run(run()) 
Example #19
Source File: delay_test.py    From micropython-async with MIT License 5 votes vote down vote up
def test(n=0):
    try:
        asyncio.run(tests[n]())
    finally:
        asyncio.new_event_loop() 
Example #20
Source File: switches.py    From micropython-async with MIT License 5 votes vote down vote up
def run():
    try:
        asyncio.run(killer())
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()
        print(tests)


# Test for the Switch class passing coros 
Example #21
Source File: asyntest.py    From micropython-async with MIT License 5 votes vote down vote up
def barrier_test():
    printexp('''0 0 0 Synch
1 1 1 Synch
2 2 2 Synch
3 3 3 Synch
4 4 4 Synch
''')
    barrier = Barrier(3, callback, ('Synch',))
    for _ in range(3):
        asyncio.create_task(report(barrier))
    asyncio.run(killer(2))

# ************ Barrier test 1 ************ 
Example #22
Source File: asyntest.py    From micropython-async with MIT License 5 votes vote down vote up
def queue_test():
    printexp('''Running (runtime = 3s):
Running foo()
Waiting for slow process.
Putting result onto queue
I've seen starships burn off the shoulder of Orion...
Time to die...
''', 3)
    asyncio.run(queue_go(3)) 
Example #23
Source File: gather.py    From micropython-async with MIT License 5 votes vote down vote up
def test(rex):
    st = exp_true if rex else exp_false
    printexp(st)
    try:
        asyncio.run(main(rex))
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()
        print()
        print('as_demos.gather.test() to run again.')
        print('as_demos.gather.test(True) to see effect of return_exceptions.') 
Example #24
Source File: auart.py    From micropython-async with MIT License 5 votes vote down vote up
def test():
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()
        print('as_demos.auart.test() to run again.') 
Example #25
Source File: aledflash.py    From micropython-async with MIT License 5 votes vote down vote up
def main(duration):
    print("Flash LED's for {} seconds".format(duration))
    leds = [pyb.LED(x) for x in range(1,4)]  # Initialise three on board LED's
    for x, led in enumerate(leds):  # Create a task for each LED
        t = int((0.2 + x/2) * 1000)
        asyncio.create_task(toggle(leds[x], t))
    asyncio.run(killer(duration)) 
Example #26
Source File: auart_hd.py    From micropython-async with MIT License 5 votes vote down vote up
def test():
    printexp()
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        asyncio.new_event_loop()
        print('as_demos.auart_hd.test() to run again.') 
Example #27
Source File: ms_timer_test.py    From micropython-samples with MIT License 5 votes vote down vote up
def test(fast=True):
    asyncio.run(main(fast)) 
Example #28
Source File: s_app.py    From micropython-iot with MIT License 5 votes vote down vote up
def main():
    clients = {'1', '2', '3', '4'}
    apps = [App(n) for n in clients]  # Accept 4 clients with ID's 1-4
    await server.run(clients, True, port=PORT, timeout=TIMEOUT) 
Example #29
Source File: s_app.py    From micropython-iot with MIT License 5 votes vote down vote up
def run():
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print('Interrupted')
    finally:
        print('Closing sockets')
        server.Connection.close_all()
        asyncio.new_event_loop() 
Example #30
Source File: s_app_cp.py    From micropython-iot with MIT License 5 votes vote down vote up
def main():
    clients = {'1', '2', '3', '4'}
    apps = [App(n) for n in clients]  # Accept 4 clients with ID's 1-4
    await server.run(clients, True, port=PORT, timeout=TIMEOUT)