0
0
PythonHow-ToBeginner · 3 min read

How to Run Functions in Parallel in Python Easily

You can run functions in parallel in Python using the threading module for lightweight tasks or the multiprocessing module for CPU-heavy tasks. Both let you start multiple functions at the same time to speed up your program.
📐

Syntax

Here are the basic ways to run functions in parallel using threading and multiprocessing modules.

  • Threading: Use threading.Thread(target=your_function, args=(args,)) to create a thread, then call start() to run it.
  • Multiprocessing: Use multiprocessing.Process(target=your_function, args=(args,)) to create a process, then call start().
python
import threading

def your_function(arg):
    # your code here
    pass

arg = None  # Define arg before using it
thread = threading.Thread(target=your_function, args=(arg,))
thread.start()

import multiprocessing

def your_function(arg):
    # your code here
    pass

arg = None  # Define arg before using it
process = multiprocessing.Process(target=your_function, args=(arg,))
process.start()
💻

Example

This example shows how to run two functions in parallel using threading. Each function prints numbers with a delay, and both run at the same time.

python
import threading
import time

def print_numbers(name):
    for i in range(1, 6):
        print(f"{name} prints {i}")
        time.sleep(0.5)

thread1 = threading.Thread(target=print_numbers, args=("Thread 1",))
thread2 = threading.Thread(target=print_numbers, args=("Thread 2",))

thread1.start()
thread2.start()

thread1.join()
thread2.join()

print("Both threads finished")
Output
Thread 1 prints 1 Thread 2 prints 1 Thread 1 prints 2 Thread 2 prints 2 Thread 1 prints 3 Thread 2 prints 3 Thread 1 prints 4 Thread 2 prints 4 Thread 1 prints 5 Thread 2 prints 5 Both threads finished
⚠️

Common Pitfalls

Common mistakes when running functions in parallel include:

  • Not calling join() to wait for threads or processes to finish, causing the main program to exit early.
  • Using threading for CPU-heavy tasks, which won't speed up due to Python's Global Interpreter Lock (GIL).
  • Sharing data between threads or processes without proper locks or communication, leading to errors.

Use multiprocessing for CPU-bound tasks and threading for I/O-bound tasks.

python
import threading
import time

def task():
    time.sleep(1)
    print("Task done")

thread = threading.Thread(target=task)
thread.start()
# Missing thread.join() means main program may exit before task finishes

# Correct way:
thread.join()  # Waits for thread to finish
Output
Task done
📊

Quick Reference

MethodUse CaseHow to StartWait for Completion
threading.ThreadI/O-bound tasks (e.g., network, file)thread = Thread(target=func); thread.start()thread.join()
multiprocessing.ProcessCPU-bound tasks (heavy calculations)process = Process(target=func); process.start()process.join()

Key Takeaways

Use threading for tasks that wait on input/output to run functions in parallel.
Use multiprocessing for CPU-heavy tasks to bypass Python's GIL and run truly in parallel.
Always call join() on threads or processes to wait for them to finish before exiting.
Avoid sharing data between threads or processes without proper synchronization.
Starting threads or processes is done by creating them with target functions and calling start().