【发布时间】:2018-02-21 16:04:40
【问题描述】:
假设我有一个 python 文件test.py:
import os
class print_args(object):
def__init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
print(x)
print(y)
print(z)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--x', nargs='+', type = str)
parser.add_argument('--y', nargs='+', type = int)
parser.add_argument('--z', type = int)
args = parser.parse_args()
print_args(args.x, args.y, args.z)
我可以从终端运行test.py,参数如下:
python3 test.py --x a b --y 2 3 --z 10
结果如预期:
a b
2 3
10
如何在终端中使用 GNU 并行运行 test.py 并带有数组参数?解决方案相当于运行:
python3 test.py --x a b --y 2 3 --z 10
python3 test.py --x a b --y 2 3 --z 20
python3 test.py --x a b --y 2 3 --z 30
python3 test.py --x a b --y 2 3 --z 40
我回答自己问题的错误尝试是:
parallel --link 'python3 test.py --x {1} --y {2} --z {3}' ::: \
> 'a b' 'a b' 'a b' 'a b' ::: \
> '2 3' '2 3' '2 3' '2 3' ::: \
> '10' '20' '30' '40'
【问题讨论】:
-
你的意思是
parallel -k echo --x "a b" --y "2 3" --z ::: 10 20 30 40 -
您的解决方案如何与
test.py配合使用? -
用
python3 test.py替换echo -
parallel -k --dry-run python3 test.py --x "a b" --y "2 3" --z ::: 10 20 30 40 -
对此有什么好处吗?还是您还有问题?
标签: python gnu-parallel