How to Run Multiple Python Scripts Simultaneously?

how-can-you-run-multiple-python-scripts-simultaneously

I often hear a question, How do I run Python code simultaneously? In this article, I will show you how can you run two or multiple Python scripts or code files at the same time simultaneously.

Course for You: Learn Python in 100 days of coding

Create Sample Python code files

There are mainly two best methods to execute two or more Python files together. But before going to those methods let’s first create two Python files that we want to run at the same time.

I am creating two codes: script1.py and script2.py. My idea is to create two simple Python files to print the current system time. Now if you see the execution starting time for both of the codes, then only you can be sure those two Python files are running simultaneously or one after another.

script1.py

By this code, I am just trying to print the current time (or when started execution) from script1.py. Added sleep() function to wait this code for 10 seconds.

import datetime
from time import sleep

current_time = datetime.datetime.now().strftime("%H:%M:%S")
sleep(10)

print("Time in Script1", current_time)
script2.py

In this second Python script, we are doing the same thing. Made just one change in the printing string.

import datetime
from time import sleep

current_time = datetime.datetime.now().strftime("%H:%M:%S")
sleep(10)

print("Time in Script2", current_time)

Once you are done with those two codes, save them inside a folder (your working directory). In my case, I saved those two codes inside this folder: D:\Python.

Method 1: Using Command Line

To start this command line method, first open a command prompt in Windows or a terminal in Linux or Mac. Then go to that directory by using cd command.

change-working-directoy-to-start-multiple-python-scripts-at-once

Now type the below command to run those two Python files at the same time.

python script1.py & python script2.py
multiple-python-scripts-is-not-running-at-once-in-command-line

In the above output, you can see that the above command is not executing those two Python files at the same time. You can see the difference in seconds. I kept each code 10 seconds sleep.

Also Read:  How to Convert String to HEX in Python [Best Methods]

script1.py started at 19:50:26 on the other hand script2.py started at 19:50:37. This 10-second gap is for 10-second sleep time in the code. That means those Python codes are running one after another (not simultaneously).

This command is useful to run multiple python scripts but not simultaneously. Another disadvantage of this command is that, both of those Python code files need to be inside same folder (in my case D:\Python).

If you really want to start multiple Python scripts at once you need to open two terminals and execute those two codes separately. For example, this time I kept script1.py inside C drive (C:\Python) and script2.py inside the same D drive (D:\Python)

Now run python script1.py inside C:\ drive terminal and python script2.py from D:\ drive terminal like below.

run-multiple-python-scripts-simultaneously-codes-in-different-folder-in-window-linux-or-mac

As you can see in the above output screen, I ran those two Python scripts from two different directories in two different terminals (command prompt), now those are running simultaneously. You can check the execution time (second) for those two scripts.

Method 2: Using Python Script

You can also run multiple Python scripts simultaneously using a Python script itself. This approach allows you to have more control and flexibility over the execution of your scripts.

# run multiple Python codes simultaneously using a Python script
import subprocess

# Define the Python scripts you want to run
scripts = ["C:/Python/script1.py", "D:/Python/script2.py"]

# Run the scripts concurrently
processes = []
for script in scripts:
    command = ["python", script]
    processes.append(subprocess.Popen(command))

# Wait for all processes to finish
for process in processes:
    process.wait()

As you can see in the above code, using this technique you can run two or multiple Python codes from any different directories. In this script, we are using the subprocess module to run your Python scripts concurrently.

Also Read:  How to Convert String to HEX in Python [Best Methods]

I saved this code as run_two_scripts.py. You can see output in the below terminal screenshot.

running-and-executing-python-code-at-the-same-time-using-python-script

As you can see in the above terminal output, both codes are running and working together exactly at the same time. I believe this is the best approach if you want to start or run multiple Python files simultaneously just by one click.

Conclusion

So can we run multiple Python scripts at once? Yes in this article I have mentioned two different methods to run multiple Python scripts simultaneously. The first one is using command prompt and the second one using a Python script itself.

FAQs

Some frequently asked questions I missed to answer in the above article. Let me cover those in this FAQ part.

Can a Python script run another Python script?

Yes, one Python script can execute another Python script. You just need to import and link those two Python scripts. In the second method, I showed you, how to run and execute two Python scripts simultaneously using the subprocess module in Python.

Can I run 2 Python scripts at the same time in Pycharm?

Yes, you can run two Python scripts simultaneously in PyCharm:

  1. Open PyCharm and load your project.
  2. Create two Run Configurations for your scripts.
  3. Run them concurrently or even run the same configuration multiple times if it has “Allow parallel run” enabled.

But I will not recommend you to run multiple scripts simultaneously in Pycharm because it is a development tool not a runtime environment.

How to run 2 Python scripts at the same time in Visual Studio Code?

You can run two Python scripts simultaneously in VS Code using the integrated terminal:

  1. Open VS Code and navigate to your script folder.
  2. Open the integrated terminal.
  3. Use cd to navigate to the script directory.
  4. Run your scripts with python script1.py & and python script2.py &.

This is it for this article. If you have any questions or suggestions regarding this article, please please drop a comment below. If you want to learn Python quickly then this Udemy course is for you: Learn Python in 100 days of coding.

Leave a comment