-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasynchronous.py
More file actions
30 lines (25 loc) · 886 Bytes
/
asynchronous.py
File metadata and controls
30 lines (25 loc) · 886 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
29
30
"""
비동기 실행 코드 - https://techblog-history-younghunjo1.tistory.com/570
Total request time is 0.48593878746032715
"""
import aiohttp # request 대신 aiohttp 라이브러리 사용
import time
import os
import threading
import asyncio
async def get_web_page(session, url: str):
print(f"Process ID: {os.getpid()} | Thread ID: {threading.get_ident()} | request to {url}")
async with session.get(url) as response:
result = await response.text()
print("============================")
async def main():
urls = [
"https://www.naver.com",
"https://www.google.com"
] * 30
async with aiohttp.ClientSession() as session:
await asyncio.gather(*[get_web_page(session, url) for url in urls])
if __name__ == '__main__':
start = time.time()
asyncio.run(main())
print(f"Total request time is {time.time() - start}")