python - 使用shell脚本的多线程

标签 python multithreading bash

我正在使用 python 脚本在图像中执行一些计算,并将获得的数组保存到 .png 文件中。我处理 3000 到 4000 张图像。为了执行所有这些操作,我在 Ubuntu 中使用 shell 脚本。它完成了工作。但无论如何,有没有办法让它变得更快。我的机器有 4 个核心。如何使用它们全部。我正在使用的脚本如下

#!/bin/bash
cd $1
for i in $(ls *.png)
do
python ../tempcalc12.py $i
done
cd ..

tempcalc12.py 是我的 python 脚本

这个问题可能很微不足道。但我对编程真的很陌生。

谢谢

最佳答案

xargs--max-procs=(或 -P) 选项,可以并行执行该工作。
以下代码最多在 4 个进程中完成这项工作。

ls *.png |  xargs -n 1 -P 4 python ../tempcalc12.py

关于python - 使用shell脚本的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27682748/

相关文章:

python - 卸载 pip 未使用的包

python - 在转换期间将 numpy ndarray 子类保留为返回值。设置 __array_priority__ 安全吗?

python - 在 Django 中使用 Pylint 时禁用某些消息

android - 多个 IntentServices 并行运行

linux - 无法通过 bash 打印到 lpr

python - 如何在 Jupyter 中将图表图像导出为 pdf?

javascript - 使用NodeJS在事件池中处理请求

python - 使用线程和/或多处理同时移动多个 turtle

bash - 守护进程的 init.d 脚本 - 无法删除 pidfile

bash - 如何仅在 bash 中存在脚本时才执行脚本?