如何使用python控制多个并发的命令行程序?

发布于 2025-01-10 21:46:06 字数 217 浏览 1 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2025-01-17 21:46:06

您可以使用 python subprocess 模块: https://docs.python.org/ 3/library/subprocess.html

作为一个简单的例子,我将运行一个 Linux 实用程序“sleep”,它除了等待给定的秒数之外什么也不做。我将并行执行此操作,看看我们是否真的可以使用子进程并行执行此操作。作为第一个设置,我这样做:

import os
import subprocess
import time

n_jobs = 5
sleeping_time_in_sec = 10
shell_command_with_arguments = ["sleep", f"{sleeping_time_in_sec}s"]

我导入“子进程”以并行启动作业。我将使用“时间”模块来测量总运行时间。我将使用“os”模块来阻止脚本,直到所有作业完成。
shell 命令看起来像“sleep 10s”,但对于子进程,您将脚本和所有参数放在列表中。

像这样提交作业:

time_start = time.time()
jobs = list()
for counter in range(n_jobs):
    process = subprocess.Popen(shell_command_with_arguments, shell=False)
    jobs.append(process)

现在,每个耗时 10 秒的 5 个作业都已提交。为了让你的脚本等到最后一个进程完成,你可以添加:

print(f"Waiting for all {n_jobs} processes to finish...")
for ip, process in enumerate(jobs):
    try:
        os.waitpid(process.pid, 0)
    except ChildProcessError:
        print(f"No more: {ip} : {process.pid}")
    else:
        print(f"DONE: {ip} : {process.pid}")

最后,我报告总运行时间:

time_end = time.time()
delta_time = time_end - time_start
print(f"Run {n_jobs} jobs taking {sleeping_time_in_sec} s each in {delta_time} s")

脚本的输出如下所示:

Waiting for all 5 processes to finish...
DONE: 0 : 88988
DONE: 1 : 88989
DONE: 2 : 88990
DONE: 3 : 88991
DONE: 4 : 88992
Finished running 5 jobs each taking 10 s in 10.022365093231201 s

如你所见,如果你运行 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:

import os
import subprocess
import time

n_jobs = 5
sleeping_time_in_sec = 10
shell_command_with_arguments = ["sleep", f"{sleeping_time_in_sec}s"]

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:

time_start = time.time()
jobs = list()
for counter in range(n_jobs):
    process = subprocess.Popen(shell_command_with_arguments, shell=False)
    jobs.append(process)

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:

print(f"Waiting for all {n_jobs} processes to finish...")
for ip, process in enumerate(jobs):
    try:
        os.waitpid(process.pid, 0)
    except ChildProcessError:
        print(f"No more: {ip} : {process.pid}")
    else:
        print(f"DONE: {ip} : {process.pid}")

Finally, I report the total running time:

time_end = time.time()
delta_time = time_end - time_start
print(f"Run {n_jobs} jobs taking {sleeping_time_in_sec} s each in {delta_time} s")

The output of the script looks like:

Waiting for all 5 processes to finish...
DONE: 0 : 88988
DONE: 1 : 88989
DONE: 2 : 88990
DONE: 3 : 88991
DONE: 4 : 88992
Finished running 5 jobs each taking 10 s in 10.022365093231201 s

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文