Skip to content
Python

멀티스레딩

threading 모듈을 사용해 멀티스레딩 구현.

#threading#multithreading

Code

python
import threading
from concurrent.futures import ThreadPoolExecutor

def task(n):
    return n * n

# Using thread pool
with ThreadPoolExecutor(max_workers=4) as pool:
    results = list(pool.map(task, range(10)))
    print(results)

# Manually create thread
threads = []
for i in range(5):
    t = threading.Thread(target=task, args=(i,))
    threads.append(t)
    t.start()
for t in threads:
    t.join()