Compound Tasks

This is a fairly advanced topic, which you should only tackle once you have mastered the basics of Jug.

A compound task is a function that builds up a potentially-complex task. Because this function might generate many intermediate tasks which are not very meaningful, using a compound task construct allows us to throw them away once they have run.

For example:

  1. @TaskGenerator
  2. def poly(a,b,c):
  3. return a*b+c
  4. @TaskGenerator
  5. def max3(values):
  6. 'Return the maximum 3 values'
  7. values.sort()
  8. return tuple(values[-3:])
  9. def buildup(numbers):
  10. results = []
  11. for n in numbers:
  12. results.append(poly(n, 2*n, poly(n,n,n)))
  13. return max3(results)
  14. # Work in Python2 & Python3
  15. numbers = list(range(192))
  16. intermediate = buildup(numbers)
  17. ...

We have a fairly complex function buildup, which takes the list of N numbers and generated 2*N+1 Tasks. These are a lot of Tasks and may make jug run slower. On the other hand, you may not want to simply have a single Task do everything:

  1. def poly(a,b,c):
  2. return a*b+c
  3. def max3(values):
  4. 'Return the maximum 3 values'
  5. values.sort()
  6. return tuple(values[-3:])
  7. @TaskGenerator
  8. def buildup(numbers):
  9. results = []
  10. for n in numbers:
  11. results.append(poly(n, 2*n, poly(n,n,n)))
  12. return max3(results)

Because, this way, you will have lost any ability to have the different calls to poly be run in parallel.

A compound task behaves like the first example until all necessary tasks have been computed:

  1. @TaskGenerator
  2. def poly(a,b,c):
  3. return a*b+c
  4. @TaskGenerator
  5. def max3(values):
  6. 'Return the maximum 3 values'
  7. values.sort()
  8. return tuple(values[-3:])
  9. @CompoundTaskGenerator
  10. def buildup(numbers):
  11. results = []
  12. for n in numbers:
  13. results.append(poly(n, 2*n, poly(n,n,n)))
  14. return max3(results)
  15. # Work in Python2 & Python3
  16. numbers = list(range(192))
  17. intermediate = buildup(numbers)
  18. ...

Basically, when Jug sees the CompoundTask, it asks is the result of all of this already available? If yes, then it is just the current result; otherwise, the function is called immediately (not at execution time, but every time the jugfile is loaded).

See Also

See also the section on mapreduce.