这是我自己的答案,因为它可能会帮助其他人。我使用文件而不是控制台来生成执行跟踪。
“&”部分是通过使用 Start-Job { } 完成的
“等待”部分是通过使用多个 Wait-Job ...
rm res.txt
$job4 = Start-Job {
$job1 = Start-Job { sleep 4; Write-output "waited 4" >> res.txt }
$job2 = Start-Job { sleep 2; write-output "waited 2" >> res.txt }
$job3 = Start-Job { sleep 6; write-output "waited 6" >> res.txt }
Wait-Job $job1
Wait-Job $job2
Wait-Job $job3
sleep 1 ; Write-output "waited plus 1" >> res.txt
}
$job8 = Start-Job {
$job5 = Start-Job { sleep 3; Write-output "waited 3" >> res.txt }
$job6 = Start-Job { sleep 5; write-output "waited 5" >> res.txt }
$job7 = Start-Job { sleep 8; write-output "waited 8" >> res.txt }
Wait-Job $job5
Wait-Job $job6
Wait-Job $job7
sleep 2 ; Write-output "waited plus 2" >> res.txt
}
Wait-Job $job4
Wait-Job $job8
sleep 1; Write-output "waited 1 more finally" >> res.txt
在 res.txt 文件上使用 tail -f,我可以验证全局序列和时间是否得到遵守。
<编辑>
最后我使用了一个python版本:
import subprocess
import threading
def bloc1():
p1 = subprocess.Popen('bash waitecho.bash 4'.split())
p2 = subprocess.Popen('bash waitecho.bash 2'.split())
p3 = subprocess.Popen('bash waitecho.bash 6'.split())
p1.wait()
p2.wait()
p3.wait()
p4 = subprocess.Popen('bash waitecho.bash 1'.split())
p4.wait()
def bloc2():
p5 = subprocess.Popen('bash waitecho.bash 3'.split())
p6 = subprocess.Popen('bash waitecho.bash 5'.split())
p7 = subprocess.Popen('bash waitecho.bash 8'.split())
p5.wait()
p6.wait()
p7.wait()
p8 = subprocess.Popen('bash waitecho.bash 2'.split())
p8.wait()
t1 = threading.Thread(target=bloc1)
t2 = threading.Thread(target=bloc2)
t1.start()
t2.start()
t1.join()
t2.join()
p9 = subprocess.Popen('bash waitecho.bash 1'.split())
p9.wait()
</编辑>