2020-07-06 22:09:13 +02:00
|
|
|
import giambio
|
2020-06-16 18:56:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def countdown(n: int):
|
|
|
|
while n > 0:
|
|
|
|
print(f"Down {n}")
|
|
|
|
n -= 1
|
2020-07-06 22:09:13 +02:00
|
|
|
await giambio.sleep(1)
|
2020-06-16 18:56:11 +02:00
|
|
|
print("Countdown over")
|
2020-07-11 17:09:31 +02:00
|
|
|
return 0
|
2020-06-16 18:56:11 +02:00
|
|
|
|
2020-07-13 22:02:31 +02:00
|
|
|
|
2020-07-06 22:09:13 +02:00
|
|
|
async def countup(stop: int, step: int = 1):
|
2020-06-16 18:56:11 +02:00
|
|
|
x = 0
|
|
|
|
while x < stop:
|
|
|
|
print(f"Up {x}")
|
|
|
|
x += 1
|
2020-07-06 22:09:13 +02:00
|
|
|
await giambio.sleep(step)
|
2020-06-16 18:56:11 +02:00
|
|
|
print("Countup over")
|
2020-07-11 17:09:31 +02:00
|
|
|
return 1
|
2020-06-16 18:56:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def main():
|
2020-07-05 08:58:23 +02:00
|
|
|
cdown = scheduler.create_task(countdown(10))
|
|
|
|
cup = scheduler.create_task(countup(5, 2))
|
2020-06-19 23:39:54 +02:00
|
|
|
print("Counters started, awaiting completion")
|
2020-07-06 22:09:13 +02:00
|
|
|
await giambio.sleep(2)
|
|
|
|
print("Slept 2 seconds, killing countup")
|
2020-07-11 17:09:31 +02:00
|
|
|
await cup.cancel()
|
2020-07-11 10:57:12 +02:00
|
|
|
print("Countup cancelled")
|
2020-07-11 17:09:31 +02:00
|
|
|
up = await cup.join()
|
|
|
|
down = await cdown.join()
|
|
|
|
print(f"Countup returned: {up}\nCountdown returned: {down}")
|
2020-06-16 18:56:11 +02:00
|
|
|
print("Task execution complete")
|
|
|
|
|
2020-07-13 22:02:31 +02:00
|
|
|
|
2020-06-16 18:56:11 +02:00
|
|
|
if __name__ == "__main__":
|
2020-07-06 22:09:13 +02:00
|
|
|
scheduler = giambio.AsyncScheduler()
|
2020-07-05 08:58:23 +02:00
|
|
|
scheduler.start(main())
|