如何使用python控制多个并发的命令行程序?
我想使用 python 程序将不同的视频发送到不同的设备。
我的计划是使用 ffmpeg 来控制视频和目标(我可以使用 os.system 对一个目标执行此操作),但我不确定如何编写并发 ffmpeg 命令,以便 6 个视频在不同的设备上同时播放设备。
最初我以为我可以使用 tmux,但我找不到如何在 python 程序中控制/访问不同 tmux 窗口的解决方案。我错过了一些明显的事情吗?
I'd like to use a python program to send different videos to different devices.
My plan is to use ffmpeg to control the video and the destination (I can do this for one destination using os.system) but I'm not sure how to write concurrent ffmpeg commands so that 6 videos are playing at the same time on different devices.
Initially I thought I could use tmux but I can't find a solution for how to control/access different tmux windows within my python program. Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 python subprocess 模块: https://docs.python.org/ 3/library/subprocess.html
作为一个简单的例子,我将运行一个 Linux 实用程序“sleep”,它除了等待给定的秒数之外什么也不做。我将并行执行此操作,看看我们是否真的可以使用子进程并行执行此操作。作为第一个设置,我这样做:
我导入“子进程”以并行启动作业。我将使用“时间”模块来测量总运行时间。我将使用“os”模块来阻止脚本,直到所有作业完成。
shell 命令看起来像“sleep 10s”,但对于子进程,您将脚本和所有参数放在列表中。
像这样提交作业:
现在,每个耗时 10 秒的 5 个作业都已提交。为了让你的脚本等到最后一个进程完成,你可以添加:
最后,我报告总运行时间:
脚本的输出如下所示:
如你所见,如果你运行 sleep 命令 5 次意甲联赛,需要50秒。但由于脚本花费了 10 多秒的时间,您可以看到作业确实是并行运行的。
You can use the python subprocess module for that: https://docs.python.org/3/library/subprocess.html
As a simple example, I will run a Linux utility 'sleep' which does nothing else than waiting for a given amount of seconds. I will do this in parallel to see that we can really do this with subprocess in parallel. As a first setup, I do:
I'm importing 'subprocess' to launch jobs in parallel. The 'time'module I'm going to use to measure the total running time. The 'os' module I'm going to use to block the script until all jobs are done.
The shell command looks like 'sleep 10s', but for subprocess you put the script and all the arguments in a list.
Submit the jobs like this:
Now all the 5 jobs each taking 10 sec have been submitted. In order to make your script wait until the last process is done, you can add:
Finally, I report the total running time:
The output of the script looks like:
As you can see, if you would run the sleep command 5 times in serie, it would take 50 s. But since the script took a little more then 10 s you can see the the jobs indeed were running in parallel.