dag has two main concept:
- Pipeline executes the functions sequentially and in order.
- Spawns executes the functions concurrently, so there is no ordering guarantee.
d := dag.New()
d.Pipeline(f1, f2, f3)
d.Run()
In the above example, f1 starts first, and after completion, f2 starts then f3.
Full example : examples/ex1/ex1.go
d := dag.New()
d.Spawns(f1, f2, f3)
d.Run()
The order of execution of f1, f2 and f3 is nondeterministic
Full example : examples/ex2/ex2.go
In this example f4 must be executed after complition of f1, f2 and f3. You can use Join method:
d := dag.New()
d.Spawns(f1, f2, f3).Join().Pipeline(f4)
d.Run()
Full example : examples/ex3/ex3.go
After pipeline we can use Then method:
d := dag.New()
d.Pipeline(f1, f2, f3).Then().Spawns(f4, f5, f6)
d.Run()
Full example : examples/ex4/ex4.go
d := dag.New()
d.Spawns(f1, f2, f3).
Join().
Pipeline(f4, f5).
Then().
Spawns(f6, f7, f8)
d.Run()
Full example : examples/ex5/ex5.go
We want to execute two pipeline concrrently, we can use pipeline.Of inside the Spawns method:
d := dag.New()
d.Spawns(pipeline.Of(f1, f3), pipeline.Of(f2, f4)).
Join().
Pipeline(f5)
d.Run()
Full example : examples/ex6/ex6.go
We can use OnComplete method after Pipeline or Spawns to notify when functions has completed.
d := dag.New()
d.Pipeline(f1, f2).OnComplete(f3).
Then().
Spawns(f1, f2).OnComplete(f4)
d.Run()
Full example : examples/ex7/ex7.go
Basically, Run() will block until all functions are done. If you don't want to be blocked, you can use RunAsync() method. It accepts a callback function, that will be called when all functions are done.
d := dag.New()
d.Pipeline(f1, f2).Then().Spawns(f3, f4)
d.RunAsync(onComplete)
Full example : examples/ex8/ex8.go