同时执行多个.py文件

编程入门 行业动态 更新时间:2024-10-26 12:31:47
本文介绍了同时执行多个.py文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在一个 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文件

本文发布于:2023-06-06 09:45:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/539013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   文件   py

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!