limit cpu 循环中进程的限制
Limit cpu limit of process in a loop
我正在尝试在多个文件的循环中执行 ffmpeg
。我一次只希望一个实例 运行,并且只使用 cpu 的 50%。我一直在尝试 cpulimit
但循环播放效果不佳。
for i in {1..9}; do cpulimit -l 50 -- ffmpeg <all the options>; done
这会同时生成所有 9 个工作,它们都属于 init
,所以我必须打开 htop
才能杀死它们。
for i in {1..9}; do ffmpeg <all the options> & cpulimit -p $! -l 50; done
这挂了,ctrl+c
继续下一个循环迭代。这些实例只能被 SIGKILL
.
杀死
使用队列是可行的方法。我使用的一个简单解决方案是 Task Spooler。您也可以使用 -threads
限制 ffmpeg 使用的核心数。这是给你的一些代码:
ts sh -c "ffmpeg -i INPUT.mp4 -threads 4 OUTPUT.mp4"
- 您可以将最大并发任务数设置为 1:
ts -S 1
- 查看当前队列只需运行
ts
如果您希望 -threads 选项对编码器产生影响,您应该将它放在 -i 参数之后,输出文件名之前 - 您当前的选项只告诉解码部分使用单线程。因此,要使用单个线程来保持全部,您需要 -threads 1 在 -i 选项之前和之后。所以你可以这样做:
ffmpeg -threads 1 -i INPUT.mp4 -threads 1 OUTPUT.mp4
您应该 运行 它在前台。这样,循环将按预期工作。
$ cpulimit --help
...
-f --foreground launch target process in foreground and wait for it to exit
这对我有用。
for file in *.mp4; do
cpulimit -f -l 100 -- ffmpeg -i "$file" <your options>
done
我正在尝试在多个文件的循环中执行 ffmpeg
。我一次只希望一个实例 运行,并且只使用 cpu 的 50%。我一直在尝试 cpulimit
但循环播放效果不佳。
for i in {1..9}; do cpulimit -l 50 -- ffmpeg <all the options>; done
这会同时生成所有 9 个工作,它们都属于 init
,所以我必须打开 htop
才能杀死它们。
for i in {1..9}; do ffmpeg <all the options> & cpulimit -p $! -l 50; done
这挂了,ctrl+c
继续下一个循环迭代。这些实例只能被 SIGKILL
.
使用队列是可行的方法。我使用的一个简单解决方案是 Task Spooler。您也可以使用 -threads
限制 ffmpeg 使用的核心数。这是给你的一些代码:
ts sh -c "ffmpeg -i INPUT.mp4 -threads 4 OUTPUT.mp4"
- 您可以将最大并发任务数设置为 1:
ts -S 1
- 查看当前队列只需运行
ts
如果您希望 -threads 选项对编码器产生影响,您应该将它放在 -i 参数之后,输出文件名之前 - 您当前的选项只告诉解码部分使用单线程。因此,要使用单个线程来保持全部,您需要 -threads 1 在 -i 选项之前和之后。所以你可以这样做:
ffmpeg -threads 1 -i INPUT.mp4 -threads 1 OUTPUT.mp4
您应该 运行 它在前台。这样,循环将按预期工作。
$ cpulimit --help
...
-f --foreground launch target process in foreground and wait for it to exit
这对我有用。
for file in *.mp4; do
cpulimit -f -l 100 -- ffmpeg -i "$file" <your options>
done