Running multiple Python files simultaneously can be quite helpful in various scenarios, such as automating tasks, running separate processes concurrently, and efficiently utilizing system resources.
In this tutorial, we’ll walk you through how to run two Python files simultaneously using different methods, including the concurrent.futures module, the threading module, and the multiprocessing module.
Step 1: Create Two Sample Python Files
Before we begin, let’s create two simple Python files that we will run simultaneously. The first file, file1.py
, will compute the sum of numbers 1 to 10, and the second file, file2.py
, will compute the factorial of a number (we’ll use 5 for illustration purposes). Create the files and save them with the following content:
file1.py:
1 2 3 4 |
def sum_numbers(n): return sum(range(1, n + 1)) print("Sum of numbers 1-10: ", sum_numbers(10)) |
file2.py:
1 2 3 4 5 6 7 |
def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) print("Factorial of 5: ", factorial(5)) |
Step 2: Run Python Files Simultaneously Using concurrent.futures
Python’s concurrent.futures module provides a high-level interface to run functions asynchronously. We’ll use the ThreadPoolExecutor
class to run our Python files in parallel. Create a new Python file, main.py
, and write the following code:
1 2 3 4 5 6 7 8 9 10 |
from concurrent.futures import ThreadPoolExecutor from file1 import sum_numbers from file2 import factorial with ThreadPoolExecutor() as executor: result1 = executor.submit(sum_numbers, 10) result2 = executor.submit(factorial, 5) print("Sum of numbers 1-10: ", result1.result()) print("Factorial of 5: ", result2.result()) |
When you run this main.py
file, it will run file1.py
and file2.py
simultaneously and print the output:
Sum of numbers 1-10: 55
Factorial of 5: 120
1 2 3 4 5 6 7 8 9 10 |
from concurrent.futures import ThreadPoolExecutor from file1 import sum_numbers from file2 import factorial with ThreadPoolExecutor() as executor: result1 = executor.submit(sum_numbers, 10) result2 = executor.submit(factorial, 5) print("Sum of numbers 1-10: ", result1.result()) print("Factorial of 5: ", result2.result()) |
Step 3: Run Python Files Simultaneously Using threading
The threading module enables running multiple threads (smaller units of a process) concurrently in the same process. To run the Python files simultaneously using threading, modify your main.py
file with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import threading from file1 import sum_numbers from file2 import factorial result1 = None result2 = None def run_file1(): global result1 result1 = sum_numbers(10) def run_file2(): global result2 result2 = factorial(5) thread1 = threading.Thread(target=run_file1) thread2 = threading.Thread(target=run_file2) thread1.start() thread2.start() thread1.join() thread2.join() print("Sum of numbers 1-10: ", result1) print("Factorial of 5: ", result2) |
When you run the updated main.py
file, it will run file1.py
and file2.py
simultaneously using threads.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import threading from file1 import sum_numbers from file2 import factorial result1 = None result2 = None def run_file1(): global result1 result1 = sum_numbers(10) def run_file2(): global result2 result2 = factorial(5) thread1 = threading.Thread(target=run_file1) thread2 = threading.Thread(target=run_file2) thread1.start() thread2.start() thread1.join() thread2.join() print("Sum of numbers 1-10: ", result1) print("Factorial of 5: ", result2) |
Output:
Sum of numbers 1-10: 55 Factorial of 5: 120 Sum of numbers 1-10: 55 Factorial of 5: 120
Step 4: Run Python Files Simultaneously Using multiprocessing
The multiprocessing module is another way to run Python files simultaneously. It uses separate processes instead of threads, which can help avoid global interpreter lock (GIL) limitations. Modify your main.py
file with the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from multiprocessing import Process, Manager from file1 import sum_numbers from file2 import factorial def run_file1(result): result['file1'] = sum_numbers(10) def run_file2(result): result['file2'] = factorial(5) if __name__ == '__main__': with Manager() as manager: result = manager.dict() process1 = Process(target=run_file1, args=(result,)) process2 = Process(target=run_file2, args=(result,)) process1.start() process2.start() process1.join() process2.join() print("Sum of numbers 1-10: ", result['file1']) print("Factorial of 5: ", result['file2']) |
Output:
Sum of numbers 1-10: 55 Factorial of 5: 120 Sum of numbers 1-10: 55 Factorial of 5: 120 Sum of numbers 1-10: 55 Sum of numbers 1-10: 55 Factorial of 5: 120 Factorial of 5: 120 Sum of numbers 1-10: 55 Factorial of 5: 120
When you run the updated main.py
file, it will run file1.py
and file2.py
simultaneously using separate processes.
Conclusion
In this tutorial, we have covered three different methods to run two Python files simultaneously using the concurrent.futures, threading, and multiprocessing modules.
Each method has its advantages and limitations, so choose the one that best fits your use case and system requirements. Now you know how to run multiple Python files concurrently to enhance the efficiency of your code.