더북(TheBook)

다음은 자식 스레드를 다섯 개 생성하는 파이썬 예제 코드다.

# Chapter 4/multithreading.py
import os
import time
import threading
from threading import Thread

def cpu_waster(i: int) -> None:
    name = threading.current_thread().getName()
    print(f"{name}가 작업 {i}를 수행 중")
    time.sleep(3)

def display_threads() -> None:
    print("-" * 10)
    print(f"현재 프로세스의 PID: {os.getpid()}")
    print(f"스레드 수: {threading.active_count()}")
    print("활성 스레드:")
    for thread in threading.enumerate():
        print(thread)


신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.