본문 바로가기

Python/Advanced

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

통신관련 멀티스레드 처리(동기방식)

 

import concurrent.futures
import threading
import requests
import time

# 각 스레드에 생성되는 객체(독립된 네임스페이스)
thread_local = threading.local()


# 세션 제공
def get_session():
    if not hasattr(thread_local, "session"): # 딕셔너리 타입으로 확인
        thread_local.session = requests.Session()
        print('>>> not hasattr()')
    return thread_local.session


def request_site(url):
    # 세션 획득
    session = get_session()

    # 세션 확인
    print(session)
    # print(session.headers)

    with session.get(url) as response:
        print(f"[Read Contents : {len(response.content)}, Status Code : {response.status_code}] from {url}")
        

def request_all_site(urls):
    # 멀티스레드 실행
    # 반드시 max_worker 개수 조절 후 session 객체 확인
    with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
        executor.map(request_site, urls)


def main():
    # 테스트 URLS
    urls = [
            "https://www.jython.org",
            "https://www.naver.com",
            "https://realpython.com/"
    ] * 3
    
    start_time = time.time()
    request_all_site(urls)
    duration = time.time() - start_time

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

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

>>> not hasattr()
<requests.sessions.Session object at 0x000002065DDD5250>
>>> not hasattr()
<requests.sessions.Session object at 0x000002065DDE1D60>
>>> not hasattr()
<requests.sessions.Session object at 0x000002065DDE19D0>
[Read Contents : 10394, Status Code : 200] from https://www.jython.org
<requests.sessions.Session object at 0x000002065DDD5250>
[Read Contents : 40221, Status Code : 200] from https://realpython.com/
<requests.sessions.Session object at 0x000002065DDE19D0>
[Read Contents : 10394, Status Code : 200] from https://www.jython.org
<requests.sessions.Session object at 0x000002065DDD5250>
[Read Contents : 201191, Status Code : 200] from https://www.naver.com
<requests.sessions.Session object at 0x000002065DDE1D60>
[Read Contents : 142304, Status Code : 200] from https://www.naver.com
<requests.sessions.Session object at 0x000002065DDE19D0>
[Read Contents : 40221, Status Code : 200] from https://realpython.com/
<requests.sessions.Session object at 0x000002065DDD5250>
[Read Contents : 10394, Status Code : 200] from https://www.jython.org
[Read Contents : 210589, Status Code : 200] from https://www.naver.com
[Read Contents : 40220, Status Code : 200] from https://realpython.com/

Downloaded 9 sites in 2.9297118186950684 seconds