简体   繁体   中英

Run multiple python file concurrently

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

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()

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 (eg 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.

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

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()
# ...

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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM