Categories

Workflows: 3 parallel and sequence

I said in the first post in this series that you could force a workflow to perform tasks in parallel or in sequence. Starting with parallel you can force parallel execution by using the parallel keyword:

workflow thursday1 {
parallel {
   1..26 | foreach {$psitem}
   65..90 | foreach {[char][byte]$psitem}
   97..122 | foreach {[char][byte]$psitem}

   1..26 | foreach {$psitem}
   65..90 | foreach {[char][byte]$psitem}
   97..122 | foreach {[char][byte]$psitem}

   1..26 | foreach {$psitem}
   65..90 | foreach {[char][byte]$psitem}
   97..122 | foreach {[char][byte]$psitem}
}
}

thursday1

In this workflow I’m printing out the numbers 1-12, the characters A-Z and a-z then repeating those actions another 2 times. The repetition is to force the parallelism to be visible. If you just run one repetition everything runs so fast that you don’t see any evidence of parallel activity.  if you look carefully at the output you will see evidence of parallelism. For instance the test I ran showed this partial sequence of results:

Y
16
17
B
18
C
19
20
D
a
b

No prizes for guessing that the keyword to force things to run sequential is sequence

workflow thursday2 {
sequence {
   1..26 | foreach {$psitem}
   65..90 | foreach {[char][byte]$psitem}
   97..122 | foreach {[char][byte]$psitem}
}
}

thursday2

No matter how many times you run this you’ll always get the same sequence of results – numbers, upper case then lower case characters.

You can mix and match

workflow thursday3 {
parallel {
    sequence {
     1..26 | foreach {$psitem}
     65..90 | foreach {[char][byte]$psitem}
     97..122 | foreach {[char][byte]$psitem}
   }
    sequence {
     1..26 | foreach {$psitem}
     65..90 | foreach {[char][byte]$psitem}
     97..122 | foreach {[char][byte]$psitem}
   }
    sequence {
     1..26 | foreach {$psitem}
     65..90 | foreach {[char][byte]$psitem}
     97..122 | foreach {[char][byte]$psitem}
   }
}
}

This runs the sequence of numbers, upper and lower case three times in parallel

or

workflow thursday4 {
sequence {
    parallel {
     1..26 | foreach {$psitem}
     65..90 | foreach {[char][byte]$psitem}
     97..122 | foreach {[char][byte]$psitem}
   }
    parallel {
     1..26 | foreach {$psitem}
     65..90 | foreach {[char][byte]$psitem}
     97..122 | foreach {[char][byte]$psitem}
   }
    parallel {
     1..26 | foreach {$psitem}
     65..90 | foreach {[char][byte]$psitem}
     97..122 | foreach {[char][byte]$psitem}
   }
}
}

thursday4

 

which runs numbers, upper and lower case in parallel – three times in sequence

Try thursday3 and thursday4 and observe the results. I’d encourage you to experiment with combinations of sequence and parallel so that you get a feel for the difference between the two types of action.

Your observations should help reinforce the message from the first post – you can’t predict the order of results when performing tasks in parallel.

Leave a Reply