본문 바로가기

Python/Advanced

CPU Bound(2) - Multi-process

CPU 연산 - 멀티 프로세스

 

from multiprocessing import current_process, Array, freeze_support, Process, Manager
import time
import os


def cpu_bound(number, total_list):

    process_id = os.getpid()
    process_name = current_process().name

    # Process 정보 출력
    print(f"Process ID: {process_id}, Process Name: {process_name}")

    total_list.append(sum(i * i for i in range(number)))


def main():

    numbers = [3_000_000 + x for x in range(30)]

    # 확인
    # print(numbers)

    # 프로세스 리스트  선언
    processes = list()

    # 프로세스 공유 매니저
    manager = Manager()

    # 리스트 획득(프로세스 공유)
    total_list = manager.list()

    start_time = time.time()

    # 프로세스 생성 및 실행
    for i in numbers:
        t = Process(name=str(i), target=cpu_bound, args=(i,total_list,))
        processes.append(t)
        t.start()

    # Join
    for process in processes:
        process.join()

    print()
    
    # 결과 출력
    print(f"Total list : {total_list}")
    print(f"Sum : {sum(total_list)}")

    # 실행 시간 종료
    duration = time.time() - start_time

    print()

    # 수행 시간
    print(f"Duration : {duration} seconds")

if __name__ == "__main__":
    # 윈도우 예외시 
    # freeze_support()
    
    # 메인 함수 실행
    main()
    
------------------------------------[result]

Process ID: 9880, Process Name: 3000000
Process ID: 17664, Process Name: 3000002
Process ID: 13880, Process Name: 3000006
Process ID: 18820, Process Name: 3000009
Process ID: 17392, Process Name: 3000020
Process ID: 8976, Process Name: 3000003
Process ID: 12016, Process Name: 3000011
Process ID: 18952, Process Name: 3000005
Process ID: 5216, Process Name: 3000015
Process ID: 14852, Process Name: 3000001
Process ID: 18600, Process Name: 3000013
Process ID: 10572, Process Name: 3000007
Process ID: 1588, Process Name: 3000008
Process ID: 18972, Process Name: 3000012
Process ID: 5112, Process Name: 3000004
Process ID: 9252, Process Name: 3000029
Process ID: 18872, Process Name: 3000024
Process ID: 19108, Process Name: 3000010
Process ID: 8600, Process Name: 3000014
Process ID: 10264, Process Name: 3000016
Process ID: 18532, Process Name: 3000026
Process ID: 10908, Process Name: 3000021
Process ID: 5344, Process Name: 3000017
Process ID: 9164, Process Name: 3000023
Process ID: 12364, Process Name: 3000027
Process ID: 11088, Process Name: 3000022
Process ID: 1488, Process Name: 3000025
Process ID: 8436, Process Name: 3000019
Process ID: 268, Process Name: 3000018
Process ID: 4568, Process Name: 3000028

Total list : [9000022500018500005, 8999995500000500000, 9000013500006500001, 9000049500090500055, 9000040500060500030, 9000031500036500014, 9000076500216500204, 9000175501140502470, 9000094500330500385, 9000130500630501015, 9000004500000500000, 9000067500168500140, 9000112500468500650, 9000058500126500091, 9000121500546500819, 9000103500396500506, 9000256502436507714, 9000211501656504324, 9000085500270500285, 9000139500720501240, 9000229501950505525, 9000184501260502870, 9000148500816501496, 9000202501518503795, 9000238502106506201, 9000193501386503311, 9000220501800504900, 9000166501026502109, 9000157500918501785, 9000247502268506930]
Sum : 270003780024375058870

Duration : 2.5116043090820312 seconds

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

CPU Bound(1) - Single process  (0) 2021.05.20
[Python] I/O Bound(2) - Asyncio  (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