Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

python - Execute multiple .py files at the same time

I have three .py files that I want to run at the same time in a Python script file. I initially called subprocess.call() three times (once for each .py file) but remembered that it blocks until the command is finished. I tried subprocess.Popen(['screen', 'python_file']) since I believe it doesn't block but when I was checking for the processes with screen -ls there was only one process running. How do I get all three programs running at the same time with a Python script? Should I be using the multiprocessing or multithreading library?

Edit: The other processes aren't supposed to finish since they're running in an infinite loop. Here's exactly what I had in my Python script file. I'm using screen because each .py file has stdout logged onto the terminal and I want to be able to see what is being logged for each one.


subprocess.Popen(['screen', './submitter.py'])
subprocess.Popen(['screen', './worker.py'])
subprocess.Popen(['screen', './tester.py'])

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use multiprocessing, you can try this:

import multiprocessing

def worker(file):
    # your subprocess code


if __name__ == '__main__':
    files = ["path/to/file1.py","path/to/file2.py","path/to/file3.py"]
    for i in files:
        p = multiprocessing.Process(target=worker, args=(i,))
        p.start()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...