我想在一个 Python 脚本文件中同时运行三个 .py 文件.我最初调用了 subprocess.call() 三次(每个 .py 文件一次),但记得它会阻塞直到命令完成.我尝试了 subprocess.Popen(['screen', 'python_file']) 因为我相信它不会阻塞,但是当我使用 screen -ls 检查进程时只有一个进程在运行.如何使用 Python 脚本同时运行所有三个程序?我应该使用 multiprocessing 还是 multithreading 库?
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?
其他进程不应该完成,因为它们在无限循环中运行.这正是我在 Python 脚本文件中的内容.我使用 screen 是因为每个 .py 文件都有标准输出登录到终端,我希望能够看到每个文件都记录了什么.
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'])
推荐答案如果你想使用multiprocessing,你可以试试这个:
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()更多推荐
同时执行多个.py文件
发布评论