- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlesson02_task_callback.py
More file actions
Latest commit
28 lines (24 loc) · 704 Bytes
/
Copy pathlesson02_task_callback.py
File metadata and controls
28 lines (24 loc) · 704 Bytes
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
# SuperFastPython.com
# example of adding a done callback function to a task
importasyncio
# custom done callback function
defhandle(task):
print(f'Task callback done: {task.done()}')
# define a coroutine for a task
asyncdeftask_coroutine():
# report a message
print('executing the task')
# suspend for a moment
awaitasyncio.sleep(1)
# custom coroutine
asyncdefmain():
# report a message
print('main coroutine')
# create and schedule the task
task=asyncio.create_task(task_coroutine())
# add a done callback function
task.add_done_callback(handle)
# wait for the task to complete
awaittask
# start the asyncio program
asyncio.run(main())