Run multiple python file concurrently

浪尽此生 提交于 2021-02-19 09:04:21

问题


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


回答1:


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



回答2:


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



回答3:


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.




回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/53865580/run-multiple-python-file-concurrently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!