brunch

매거진 백엔드

You can make anything
by writing

C.S.Lewis

by 내가 사는 세상 Jun 19. 2024

멀티 프로세싱, 멀티 스레딩

목차


기초개념


1. 멀티 프로세싱(Multi Processing) #process  #concurrent

    - multiprocessing 모듈

    - concurrent 모듈


2. 멀티 스레딩(Multi Threading)  #threading  #concurrent

    - threading 모듈

    - concurrent 모듈




기초 개념

프로세스(process) = 실행중인 프로그램 : 멀티코어 시스템에서 병렬 실행 가능

스레드(thread) = 동일한 프로세스 내에서 실행되는 작은 실행 단위





1. 멀티 프로세싱(Multi Processing) #process  #concurrent

- cpu 집약적인 처리에 유용



multiprocessing 모듈


import multiprocessing

import time


def worker(num):

    """스레드가 실행할 함수"""

    print(f'Worker {num} 시작')

    time.sleep(2)

    print(f'Worker {num} 완료')


if __name__ == "__main__":

    processes = []

    for i in range(5):

        process = multiprocessing.Process(target=worker, args=(i,))

        processes.append(process)

        process.start()

    for process in processes:

        process.join()

    print("모든 작업 완료")




concurrent 모듈


from concurrent.futures import ProcessPoolExecutor

import time


def square(n):

    time.sleep(1)

    return n * n

if __name__ == '__main__':

    with ProcessPoolExecutor(max_workers=4) as executor:

        numbers = [1, 2, 3, 4, 5]

        results = list(executor.map(square, numbers))

    print(results)




2. 멀티 스레딩(Multi Threading)  #threading  #concurrent


- IO바운드에 유용(네트워크 통신 등)

- GIL(Global Interpreter Lock) : 하나의 스레드만 사용가능하게끔 제한해서 cpu연산 집약에선 성능향상X




threading 모듈


import threading

import time


def worker(num):

    """스레드가 실행할 함수"""

    print(f'Worker {num} 시작')

    time.sleep(2)

    print(f'Worker {num} 완료')


if __name__ == "__main__":

    threads = []

    for i in range(5):

        thread = threading.Thread(target=worker, args=(i,))

        threads.append(thread)

        thread.start()

    for thread in threads:

        thread.join()

    print("모든 작업 완료")



concurrent 모듈


from concurrent.futures import ThreadPoolExecutor

import time


def square(n):

    time.sleep(1)

    return n * n


if __name__ == '__main__':

    with ThreadPoolExecutor(max_workers=4) as executor:

        numbers = [1, 2, 3, 4, 5]

        results = list(executor.map(square, numbers))

    print(results)


매거진의 이전글 Django : views -> templates
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari