본문 바로가기

Python/Advanced

[Python] I/O Bound(2) - Asyncio

통신관련 비동기 처리

 

import asyncio
import aiohttp
# import requests # 패키지는 동기방식이라서 사용불가.
import time


async def request_site(session, url):

    # 세션 확인
    # print(session)
    # print(session.headers)
    
    async with session.get(url) as response:
        # status_code 동기화 문제
        print("Read Contents {}, from {}, stauts:{}".format(response.content_length, url, response.status))
        # print("*****Read Contents {0}, from {1}".format(response.status, url))


async def request_all_site(urls):
    async with aiohttp.ClientSession() as session:
        # 작업 목록
        tasks = []
        for url in urls:
            # 태스크 목록 생성
            task = asyncio.ensure_future(request_site(session, url))
            tasks.append(task)

        # 태스크 확인
        # print(*tasks)
        # print(tasks)
        
        # 태스크 완료 시 까지  태스크 사용
        await asyncio.gather(*tasks, return_exceptions=True)


def main():

    urls = [
            "https://www.jython.org",
            "https://www.naver.com",
            "https://realpython.com/"
    ] * 3
    
    start_time = time.time()
    asyncio.get_event_loop().run_until_complete(request_all_site(urls))
    # 실행2(파이썬 3.7 이상)
    # asyncio.run(request_all_site(urls)) # 3.8+ 에서 버그 있음
    duration = time.time() - start_time

    print(f"Downloaded {len(urls)} sites in {duration} seconds")

if __name__ == "__main__":
    main()
    
-----------------------------------------------[result]    

Read Contents 3586, from https://www.jython.org, stauts:200
Read Contents 3586, from https://www.jython.org, stauts:200
Read Contents 3586, from https://www.jython.org, stauts:200
Read Contents None, from https://www.naver.com, stauts:200
Read Contents 40705, from https://www.naver.com, stauts:200
Read Contents None, from https://realpython.com/, stauts:200
Read Contents None, from https://realpython.com/, stauts:200
Read Contents None, from https://realpython.com/, stauts:200
Read Contents 31924, from https://www.naver.com, stauts:200

Downloaded 9 sites in 0.8508691787719727 seconds

'Python > Advanced' 카테고리의 다른 글

CPU Bound(2) - Multi-process  (0) 2021.05.20
CPU Bound(1) - Single process  (0) 2021.05.20
[Python] I/O Bound(2) - Asyncio basic  (0) 2021.05.20
[Python] I/O Bound(2) - Multiproecessing  (0) 2021.05.20
[Python] I/O Bound(2) - Multithreading  (0) 2021.05.20