7

how to run multiple files of python simultaneously

I have three files pop.py pop1.py pop2.py i want to run this file concurrently this files are getting run one by one python code to run all files

2
  • 1
    You need to start different processes (threads aren't enough) so the easiest way is to start them manually. Commented Dec 20, 2018 at 9:18
  • 1
    Welcome to Stackoverfow. You are much more likely to get relevant answers if you can include some code in the question along with a description of what you expect to happen and what actually happens. This site is mostly about helping people to get their existing code working. even if it's a shell script - just let us see what you tried. Commented Dec 20, 2018 at 9:31

7 Answers 7

9

You can easily accomplish this with the subprocess module.

import subprocess

process1 = subprocess.Popen(["python", "pop.py"]) # Create and launch process pop.py using python interpreter
process2 = subprocess.Popen(["python", "pop1.py"])
process3 = subprocess.Popen(["python", "pop2.py"])

process1.wait() # Wait for process1 to finish (basically wait for script to finish)
process2.wait()
process3.wait()
Sign up to request clarification or add additional context in comments.

2 Comments

I want to run each file simultaneously
this runs all of them simultaneously, the only delay between the start times of the processes is the execution of the Popen function which is insignificant
1

Does it have to be a python solution? With the problem as stated, it might be easiest to just start all three in bash:

python pop.py &
python pop1.py &
python pop2.py &
wait # wait for all three to finish, if needed

While this solution runs them concurrently, you should think about why you want them to be concurrent. Are you trying to parallelize your computation? Are the processes communicating (e.g. a Producer/Consumer pattern)? Are there any dependencies between them? In all but the simplest cases, you would usually be better served by bundling all three python modules together into a python package and adding some runner code which imports all three, starts each as a thread (see oren revenge's answer), and handles any inter-process communication.

Comments

1

I would recommend to read about threading within Python. You should think about rearranging your code in one file.

PSEUDOCODE

import threading

class Pop(threading.Thread):
    def run(self):
        # Content from "pop.py"
        # Maybe some some changes are needed


class Pop1(threading.Thread):
    def run(self):
        # Content from "pop1.py"


# ...

pop = Pop()
pop1 = Pop1()
# ...

pop.start()
pop1.start()
# ...

Comments

1
import test1,test2
from threading import Thread

Thread(target=test2.main).start()
Thread(target=test1.main).start()

This script runs test1.py and test2.py concurrently. Hope this helps.

Comments

0

Make main() function in every python file and then import all the files in your main file. Then call all main functions.

from . import pop
from . import pop1
# and so on

# and now call all main functions
pop.main()
pop1.main()
# and so on

Comments

0

create a Shell file like this

 python pop.py
 python pop1.py
 python pop2.py

and run .sh file. .sh Run multiple file one by one

1 Comment

As written, this would run the three files sequentially.
0

See 3 other methods below much better, that also run the .py, not just open !

Method 1: https://gist.github.com/me-suzy/746193a040ae510429c8bcdda73325ab

Method 2: https://gist.github.com/me-suzy/b9301c9acd9d5867185782bd38ae3161

Method 3: https://gist.github.com/me-suzy/7e21b0ae011fbb09a97e89fc6d727dc8

Most simple method:

Run bebe-ro.bat (change YOUR-USER of your PC)

----bebe-ro.bat----

@echo off
echo Pornirea scripturilor

start /b c:\Users\YOUR-USER\AppData\Local\Programs\Python\Python312\python.exe "e:\File-1.py"
start /b c:\Users\YOUR-USER\AppData\Local\Programs\Python\Python312\python.exe "e:\File-2.py"
start /b c:\Users\YOUR-USER\AppData\Local\Programs\Python\Python312\python.exe "e:\File-3.py"
start /b c:\Users\YOUR-USER\AppData\Local\Programs\Python\Python312\python.exe "e:\File-4.py"
start /b c:\Users\YOUR-USER\AppData\Local\Programs\Python\Python312\python.exe "e:\File-5.py"


echo Toate scripturile au fost pornite. Apăsați orice tastă pentru a închide această fereastră.
pause > nul

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.