ucharles / gachatory

https://www.gachatory.com
0 stars 0 forks source link

[Python] 스크래퍼 성능 향상 (이미지 다운로드 관련) #86

Closed ucharles closed 6 months ago

ucharles commented 9 months ago

문제

해결책 (w. ChatGPT)

ucharles commented 7 months ago

예제 코드

# 병렬 처리를 위한 ThreadPoolExecutor 사용
# max_workers는 동시에 실행할 스레드 수, 최대 5개
with ThreadPoolExecutor(max_workers=5) as executor:
    future_to_url = {
        # download_image 함수를 실행하고 결과를 future에 저장
        executor.submit(download_image, url, filename): url
        for url, filename in download_tasks
    }
    for future in as_completed(future_to_url):
        url = future_to_url[future]
        try:
            path = future.result()
            if path:
                result.append(path)
                logging.info(f"{url} downloaded to {path}")
        except Exception as exc:
            print(f"{url} generated an exception: {exc}")