1

Is there a simple equivalent to the following bash sequence in powershell or cmd (bat) ?

It is two blocks of 3 commands running in parallel, with a final 4th command in each block, both block beeing executed in parallel, then a final command is executed after both block completed.

(
(sleep 4; echo waited 4 ) &
(sleep 2; echo waited 2 ) &
(sleep 6; echo waited 6 ) &
wait
(sleep 1; echo waited plus 1 )
) &
(
(sleep 3; echo waited 3 ) &
(sleep 5; echo waited 5 ) &
(sleep 8; echo waited 8 ) &
wait
(sleep 2; echo waited plus 2 )
) &

wait
(sleep 1; echo waited 1 more finally)

which produces:

waited 2
waited 3
waited 4
waited 5
waited 6
waited plus 1
waited 8
waited plus 2
waited 1 more finally

I have found this https://stackoverflow.com/questions/71366320/in-powershell-how-to-wait-for-parallel-jobs-to-finish-before-proceeding which is the closest regarding synchronisation, but is not simple considering the imbrication needed.

adMC
  • 11
  • 4

1 Answers1

0

Here is my own answer, as it might help others. I use a file instead of the console to produce the execution trace.

The "&" part is done by using Start-Job { } The "wait" part is done by using multiple 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

Using a tail -f on the res.txt file, I could verify that the global sequence and timings are respected.

<Edit> Finally I used a python version:

    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()

</edit>

adMC
  • 11
  • 4