1# test micropython.schedule() function 2 3import micropython 4 5try: 6 micropython.schedule 7except AttributeError: 8 print("SKIP") 9 raise SystemExit 10 11# Basic test of scheduling a function. 12 13 14def callback(arg): 15 global done 16 print(arg) 17 done = True 18 19 20done = False 21micropython.schedule(callback, 1) 22while not done: 23 pass 24 25# Test that callbacks can be scheduled from within a callback, but 26# that they don't execute until the outer callback is finished. 27 28 29def callback_inner(arg): 30 global done 31 print("inner") 32 done += 1 33 34 35def callback_outer(arg): 36 global done 37 micropython.schedule(callback_inner, 0) 38 # need a loop so that the VM can check for pending events 39 for i in range(2): 40 pass 41 print("outer") 42 done += 1 43 44 45done = 0 46micropython.schedule(callback_outer, 0) 47while done != 2: 48 pass 49 50# Test that scheduling too many callbacks leads to an exception. To do this we 51# must schedule from within a callback to guarantee that the scheduler is locked. 52 53 54def callback(arg): 55 global done 56 try: 57 for i in range(100): 58 micropython.schedule(lambda x: x, None) 59 except RuntimeError: 60 print("RuntimeError") 61 done = True 62 63 64done = False 65micropython.schedule(callback, None) 66while not done: 67 pass 68