diff --git a/.gitignore b/.gitignore index c8d4bef..287fa1a 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ # Go workspace file go.work -*.prof \ No newline at end of file +*.prof +example \ No newline at end of file diff --git a/cmd/example.go b/cmd/example.go index 5873bb5..686116a 100644 --- a/cmd/example.go +++ b/cmd/example.go @@ -3,13 +3,15 @@ package main import ( "context" "fmt" + "log" + "os" "runtime" gotaskflow "github.com/noneback/go-taskflow" ) func main() { - executor := gotaskflow.NewExecutor(uint(runtime.NumCPU())) + executor := gotaskflow.NewExecutor(uint(runtime.NumCPU() - 1)) tf := gotaskflow.NewTaskFlow("G") A, B, C := gotaskflow.NewTask("A", func(ctx *context.Context) { @@ -38,8 +40,51 @@ func main() { C.Succeed(A1) C.Succeed(B1) - tf.Push(A, B, C) - tf.Push(A1, B1, C1) + subflow := gotaskflow.NewSubflow("sub1", func(sf *gotaskflow.Subflow) { + A2, B2, C2 := + gotaskflow.NewTask("A2", func(ctx *context.Context) { + fmt.Println("A2") + }), + gotaskflow.NewTask("B2", func(ctx *context.Context) { + fmt.Println("B2") + }), + gotaskflow.NewTask("C2", func(ctx *context.Context) { + fmt.Println("C2") + }) + A2.Precede(B2) + C2.Precede(B2) + sf.Push(A2, B2, C2) + }) + + subflow2 := gotaskflow.NewSubflow("sub2", func(sf *gotaskflow.Subflow) { + A3, B3, C3 := + gotaskflow.NewTask("A3", func(ctx *context.Context) { + fmt.Println("A3") + }), + gotaskflow.NewTask("B3", func(ctx *context.Context) { + fmt.Println("B3") + }), + gotaskflow.NewTask("C3", func(ctx *context.Context) { + fmt.Println("C3") + // time.Sleep(10 * time.Second) + }) + A3.Precede(B3) + C3.Precede(B3) + sf.Push(A3, B3, C3) + }) + subflow.Precede(B) + subflow.Precede(subflow2) + + tf.Push(A, B, C) + tf.Push(A1, B1, C1, subflow, subflow2) executor.Run(tf).Wait() + fmt.Println("Print DOT") + if err := gotaskflow.Visualizer.Visualize(tf, os.Stdout); err != nil { + log.Fatal(err) + } + fmt.Println("Print Flamegraph") + if err := executor.Profile(os.Stdout); err != nil { + log.Fatal(err) + } } diff --git a/readme.md b/readme.md index dcb728a..585ac83 100644 --- a/readme.md +++ b/readme.md @@ -35,7 +35,7 @@ import ( ) func main() { - executor := gotaskflow.NewExecutor(runtime.NumCPU() - 1) + executor := gotaskflow.NewExecutor(uint(runtime.NumCPU() - 1)) tf := gotaskflow.NewTaskFlow("G") A, B, C := gotaskflow.NewTask("A", func(ctx *context.Context) { @@ -100,19 +100,19 @@ func main() { subflow.Precede(B) subflow.Precede(subflow2) - tf := gotaskflow.NewTaskFlow("G") tf.Push(A, B, C) tf.Push(A1, B1, C1, subflow, subflow2) - exector.Run(tf).Wait() + executor.Run(tf).Wait() fmt.Println("Print DOT") if err := gotaskflow.Visualizer.Visualize(tf, os.Stdout); err != nil { log.Fatal(err) } fmt.Println("Print Flamegraph") - if err :=exector.Profile(os.Stdout);err != nil { + if err := executor.Profile(os.Stdout); err != nil { log.Fatal(err) } } + ``` ### How to use visualize taskflow ```go