-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial.py
38 lines (33 loc) · 1018 Bytes
/
tutorial.py
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
29
30
31
32
33
34
35
36
37
38
import asyncio
async def msg(text):
await asyncio.sleep(0.1)
print(text)
return text
async def long_operation(i):
print(i + ' long_operation started')
await asyncio.sleep(3)
print(i +'long_operation finished')
return i
if __name__ == "__main__":
loop = asyncio.get_event_loop()
task = []
t1 = loop.create_task(msg('first'))
t2 = loop.create_task(long_operation('1'))
loop.run_until_complete(msg('second'))
print("finished second")
#loop.run_until_complete(msg('third'))
#print("finished all")
for task in asyncio.Task.all_tasks():
print(task)
print(task.done())
print(task.cancelled())
if not task.done() and not task.cancelled():
print("cancelling" )
task.cancel()
for task in asyncio.Task.all_tasks():
print(task)
#loop.run_until_complete(long_operation('2'))
print(t1.result())
print(t2.result())
#task.append(t)
#loop.run_until_complete(asyncio.wait(task))