-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpa_asyncio.py
More file actions
50 lines (42 loc) · 1.11 KB
/
Copy pathpa_asyncio.py
File metadata and controls
50 lines (42 loc) · 1.11 KB
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
44
45
46
47
48
49
50
# -*- coding:utf-8 -*-
import time
'''
def job(t):
print("Start job", t)
time.sleep(t)
print("Job", t, "takes", t, 's')
def main():
[job(t) for t in range(1,3)]
t1 = time.time()
main()
print("No async total time: ", time.time()-t1)
'''
import asyncio
'''
async def job(t):
print("Start job", t)
await asyncio.sleep(t)
print("Job", t, "takes", t, 's')
async def main(loop):
tasks = [
loop.create_task(job(t)) for t in range(1,3)
]
await asyncio.wait(tasks)
'''
import requests
import aiohttp
URL = 'https://morvanzhou.github.io/'
async def job(session):
response = await session.get(URL) # 等待并切换
return str(response.url)
async def main(loop):
async with aiohttp.ClientSession() as session:
tasks = [loop.create_task(job(session)) for _ in range(2)]
finished, unfinished = await asyncio.wait(tasks)
all_results = [r.result() for r in finished] # 获取所有结果
print(all_results)
t1 = time.time()
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()
print("Async total time:", time.time()-t1)