CodeBlanks Logo Icon CodeBlanks

Process

Learn to create processes with multiprocessing.Process.

What is multiprocessing.Process?

multiprocessing.Process starts a new operating system process. Each process has its own memory space, so it is not affected by the GIL (Global Interpreter Lock).

You create a Process by passing a function through the target argument. p.start() launches the process and p.join() waits for it to finish. To share data between processes you use Queue or Manager. This is the primary way to parallelize CPU-bound work.

Process lifecycle

A Process lifecycle has three steps: create, start, wait. You create with Process(target=func), start with .start(), and wait for completion with .join().

Without .start() the process never runs. Without .join() the main program continues without waiting for the process; this can cause early exits. So always calling join after start is a good habit. For inter-process communication you use Queue.

Practice Call greet with Process

Import with from multiprocessing import Process. Define a greet function that does print("merhaba"). Create a process with Process(target=greet), run it with .start() and .join().

Starter code

main.py
# write the greet function
# create and run a process

Sample solution

main.py
from multiprocessing import Process

def greet():
    print("merhaba")

p = Process(target=greet)
p.start()
p.join()

Expected output

merhaba

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the Process target parameter take?

main.py
from multiprocessing import Process
p = Process(target=func)
  • A memory address
  • The process ID
  • The function to run Correct answer
  • A file name

Explanation: target takes the function the process will run.

2 Match Process concepts

Term Meaning
Process New process
target Function
start Launches
join Waits

3 Complete the Process definition

Question
p = ____(target=greet)
Answer
p = Process(target=greet)

Explanation: Process(target=func) creates a new process.

All lessons in Expert