multiprocessing Basics
Use multiple CPU cores.
Why multiprocessing?
The GIL stops threads from running Python bytecode in parallel. The multiprocessing module sidesteps this by launching separate processes, each with its own interpreter and GIL, so CPU-bound work truly runs on multiple cores.
import multiprocessing as mp
print('CPU cores available:', mp.cpu_count())Starting a Process
Process(target=func) mirrors the threading API. Call start() to launch and join() to wait.
import multiprocessing as mp
def worker():
print('hello from a child process')
if __name__ == '__main__':
p = mp.Process(target=worker)
p.start()
p.join()
print('parent done')All lessons in this course
- Threads and the GIL
- ThreadPoolExecutor
- multiprocessing Basics
- Sharing Data Safely