본문 바로가기

Threadpoolexecutor

(3)
[Python] 병행성(Concurrency) - Futures(2) 2가지 패턴 실습 concurrent.futures - wait, as_completed import os import time from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, wait, as_completed WORK_LIST = [100000000, 10000000, 1000000, 100000] # 동시성 합계 계산 메인 함수 # 누적 합계 함수(제레네이터) def sum_generator(n): return sum(n for n in range(1, n+1)) # wait - 모든 작업이 끝날때까지 기다림 # as_completed - 먼저 끝난 작업결과를 반환함 def main(): # Worker Count wor..
[Python] 병행성(Concurrency) - Futures(1) Futures 동시성 비동기 작업 실행 지연시간(Block) CPU 및 리소스 낭비 방지 -> (File)Network I/O 관련 작업 -> 동시성 활용 권장 비동기 작업과 적합한 프로그램일 경우 압도적으로 성능 향상 futures : 비동기 실행을 위한 API를 고수준으로 작성 -> 사용하기 쉽도록 개선 concurrent.Futures 1. 멀티스레딩/멀티프로세싱 API 통일 -> 매우 사용하기 쉬움 2. 실행중이 작업 취소, 완료 여부 체크, 타임아웃 옵션, 콜백추가, 동기화 코드 매우 쉽게 작성 -> Promise 개념 GIL : 두 개 이상의 스레드가 동시에 실행 될 때 하나의 자원을 엑세스 하는 경우 -> 문제점을 방지하기 위해 GIL 실행 , 리소스 전체에 락이 걸린다. -> Context..
[Python] Thread(3) - ThreadPoolExecutor import logging from concurrent.futures import ThreadPoolExecutor import time def task(name, n): logging.info('***Sub-Thread %s: starting', name) result = 0 for i in range(n): result += i logging.info('***Sub-Thread %s: finished result: %d', name, result) return result def main(): # Logging format 설정 format = "%(asctime)s: %(message)s" logging.basicConfig(format=format, level=logging.INFO, datefm..