简体   繁体   English

这两个bash并行化语法有什么区别?

[英]What's the difference between these two bash parallelization syntax?

Value "4" below is the number of CPU threads. 值“4”以下是CPU线程数。 Idea is to run the tasks in batch of 4 and wait until the current batch is finished before starting the next batch. 想法是以批次4运行任务并等到当前批次完成后再开始下一批次。

Syntax 1: 语法1:

while read something; do
((++i%4==0)) && wait
(
 task using something as input;
)
done < input_file.txt

Syntax 2: 语法2:

while read something; do
((i=i%4)); ((i++==0)) && wait
(
 task using something as input;
)
done < input_file.txt

To me they both work the same except the second one is longer. 对我来说,他们都工作相同,除了第二个更长。 But when running in the cloud (AWS ubuntu 14.04), only syntax 1 worked. 但是当在云中运行时(AWS ubuntu 14.04),只有语法1有效。 The syntax2 threw a generic syntax error at "((i=i%4));" syntax2在“((i = i%4))”处抛出了一般语法错误;“ step and it became a mystery. 一步,它变成了一个谜。

"The second one is longer" doesn't help since you used pseudocode. “第二个更长”没有用,因为你使用了伪代码。

Maybe this will help: 也许这会有所帮助:

while read x; do ((i=++i%4)) || wait; sleep $x & done < input_file.txt

My input_file.txt : 我的input_file.txt

10
9
8
7
6
5
4
3
2
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM