-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoroutine_test.py
43 lines (33 loc) · 991 Bytes
/
coroutine_test.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
39
40
41
42
43
import asyncio
import time
# async def crawl_page(url):
# print('crawling {}'.format(url))
# sleep_time = int(url.split('_')[-1])
# await asyncio.sleep(sleep_time)
# print('OK {}'.format(url))
# async def main(urls):
# tasks = [asyncio.create_task(crawl_page(url)) for url in urls]
# await asyncio.gather(*tasks)
# asyncio.run(main(['url_1', 'url_2', 'url_3', 'url_4']))
async def work_1():
await asyncio.sleep(1)
return 1
async def work_2():
await asyncio.sleep(2)
return 2 / 0
async def work_3():
print("Work 3 start")
await asyncio.sleep(3)
print("Work 3 end")
return 3
async def main():
task_1 = asyncio.create_task(work_1())
task_2 = asyncio.create_task(work_2())
task_3 = asyncio.create_task(work_3())
await asyncio.sleep(2)
# task_3.cancel()
res = await asyncio.gather(task_1, task_2, task_3, return_exceptions=True)
print(res)
print(time.time())
print(time.time())
asyncio.run(main())