VF1*_*VF1 14 xargs parallelism
假设我有两个资源,命名为0
和1
,只能以独占方式访问。
有没有办法恢复xargs
启动的“并行处理器”的“索引”,以便将其用作免费互斥服务?例如,考虑以下并行计算:
$ echo {1..8} | xargs -d " " -P 2 -I {} echo "consuming task {}"
consuming task 1
consuming task 2
consuming task 3
consuming task 4
consuming task 5
consuming task 6
consuming task 7
consuming task 8
Run Code Online (Sandbox Code Playgroud)
我的问题是是否存在一个神奇的词,比如说index
,输出看起来像
$ echo {1..8} | xargs -d " " -P 2 -I {} echo "consuming task {} with resource index"
consuming task 1 with resource 0
consuming task 2 with resource 1
consuming task 3 with resource 1
consuming task 4 with resource 1
consuming task 5 with resource 0
consuming task 6 with resource 1
consuming task 7 with resource 0
consuming task 8 with resource 0
Run Code Online (Sandbox Code Playgroud)
唯一的保证是最多只有一个进程在使用资源,0
并且对于1
. 基本上,我想将此索引传达给子进程,该子进程将遵守仅使用它被告知的资源的规则。
当然,最好将此扩展到两个以上的资源。检查文档,xargs
可能无法做到这一点。是否有最小的等效解决方案?使用/清理文件作为假锁是不可取的。
mur*_*uru 21
如果您使用的是GNU xargs,则有--process-slot-var
:
--process-slot-var
= environment-variable-name
将环境变量environment-variable-name设置为每个正在运行的子进程中的唯一值。每个值都是一个十进制整数。一旦子进程退出,值就会被重用。例如,这可以用于基本的负载分配方案。
因此,例如:
~ echo {1..9} | xargs -n2 -P2 --process-slot-var=index sh -c 'echo "$index" "$@" "$$"' _
0 1 2 10475
1 3 4 10476
1 5 6 10477
0 7 8 10478
1 9 10479
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2401 次 |
最近记录: |