예제

import os,time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, wait, as_completed

WORK_LIST = [100000, 1000000, 10000000, 100000000] # 함수를 실행하도록 하기 나름입니다.

# 동시성 합계 계산 메인 함수
# 누적 합계 함수(제너레이터)
def sum_generator(n):
    return sum(n for n in range(1, n+1))

def main():
    # Worker Count
    worker = min(10, len(WORK_LIST))
    #시작 시간
    start_tm = time.time()
    #futures
    futures_list = []

    #결과 건수
    # ProcessPool Excutor
    with ProcessPoolExecutor() as excutor:
        for work in WORK_LIST:
            # future 반환 (실행 X)
            future = excutor.submit(sum_generator, work)
            # 스케쥴링
            futures_list.append(future)
            # 스케쥴링 확인
            print('Scheduled for {} : {}'.format(work, future))

        result = wait(futures_list, timeout=7)
        # 성공 한거만
        print('Completed Task : ' + str(result.done))
        # 실패
        print('Pending onces after waiting for 7seconds : ' + str(result.not_done))
        # 결과 값 출력
        print([future.result() for future in result.done])

    #종료 시간
    end_tm = time.time() - start_tm
    msg = '\\n Result -> {} Time : {:.2f}s'
    #최종 결과 출력
    print(msg.format(list(result), end_tm))

# 실행
if __name__ == '__main__':
    main()
import os,time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor, wait, as_completed

WORK_LIST = [100000, 1000000, 10000000, 100000000] # 함수를 실행하도록 하기 나름입니다.

# 동시성 합계 계산 메인 함수
# 누적 합계 함수(제너레이터)
def sum_generator(n):
    return sum(n for n in range(1, n+1))

def main():
    # Worker Count
    worker = min(10, len(WORK_LIST))
    #시작 시간
    start_tm = time.time()
    #futures
    futures_list = []

    #결과 건수
    # ProcessPool Excutor
    with ThreadPoolExecutor() as excutor:
        for work in WORK_LIST:
            # future 반환 (실행 X)
            future = excutor.submit(sum_generator, work)
            # 스케쥴링
            futures_list.append(future)
            # 스케쥴링 확인
            print('Scheduled for {} : {}'.format(work, future))

        # #wait 결과 출력
        # result = wait(futures_list, timeout=7)
        # # 성공 한거만
        # print('Completed Task : ' + str(result.done))
        # # 실패
        # print('Pending onces after waiting for 7seconds : ' + str(result.not_done))
        # # 결과 값 출력
        # print([future.result() for future in result.done])

        # as_completed 결과 출력
        for future in as_completed(futures_list):
            result = future.result()
            done = future.done()
            cancelled = future.cancelled

            #future 결과 확인
            print('Future Result : {}, Done : {}'.format(result, done))
            print('Future Cancelled : {}'.format(cancelled))

    #종료 시간
    end_tm = time.time() - start_tm
    msg = '\\n Time : {:.2f}s'
    #최종 결과 출력
    print(msg.format(end_tm))

# 실행
if __name__ == '__main__':
    main()