2020-06-19 23:39:54 +02:00
|
|
|
from giambio import AsyncScheduler, sleep
|
2020-06-16 18:56:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def countdown(n: int):
|
|
|
|
while n > 0:
|
|
|
|
print(f"Down {n}")
|
|
|
|
n -= 1
|
|
|
|
await sleep(1)
|
|
|
|
print("Countdown over")
|
|
|
|
|
|
|
|
|
|
|
|
async def countup(stop, step: int or float = 1):
|
|
|
|
x = 0
|
|
|
|
while x < stop:
|
|
|
|
print(f"Up {x}")
|
|
|
|
x += 1
|
|
|
|
await sleep(step)
|
|
|
|
print("Countup over")
|
|
|
|
|
|
|
|
|
|
|
|
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-05 08:58:23 +02:00
|
|
|
await sleep(2)
|
|
|
|
print("Slept 1 second, killing countdown")
|
|
|
|
await cdown.cancel()
|
2020-06-19 23:39:54 +02:00
|
|
|
await cup.join()
|
|
|
|
await cdown.join()
|
2020-06-16 18:56:11 +02:00
|
|
|
print("Task execution complete")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
scheduler = AsyncScheduler()
|
2020-07-05 08:58:23 +02:00
|
|
|
scheduler.start(main())
|
2020-06-16 18:56:11 +02:00
|
|
|
|