Dependency-based task execution in Go

I think this is more a question of logic than myself. I want to find a way to accomplish a task based on its dependencies in go.

Task workflow

Given the workflow above: Jobs 1,2,3 and 4 can be run asynchronously at the same time. Task 10 will be completed after tasks 1 and 2 are completed. Task 11 will be completed when task 3 and 4 are completed. Task 12 will be completed when tasks 11 and 12 are completed. Task 100 will be completed when tasks 10 and 11 are completed.

I use go channels to run parallel executions and want to find an efficient way to manage dependencies. I know that I may have some kind of flag and a table for controlling execution, but ideally I would like to do this in memory to avoid database calls to manage such a thing. I also understand that there are several ways to do this, but I would like to hear your ideas, because I'm sure there is a better way to do than the ways that I could come up with so far.

+6
source share
1 answer

There was an interesting reddit thread in this thread in response to the article " How to Wait Until All Mountains Go Finish Before Continuing ."

Correct article - How to wait until all Gorutinians finish execution before continuing, part two: “Correcting my tricks” and illustrates several ways to wait for the Goroutians to do another task.

Depending on the type of information you have for synchronizing tasks, sync.WaitGroup is a good candidate (as in this example ).

But:

When you know the number of messages expecting you can also count them when you need to finish. Here WaitGroup is redundant and confusing.

This will be blocked until all 3 messages are received:

 func main() { messages := make(chan int) go func() { time.Sleep(time.Second * 3) messages <- 1 }() go func() { time.Sleep(time.Second * 2) messages <- 2 }() go func() { time.Sleep(time.Second * 1) messages <- 3 }() for i := 0; i < 3; i++ { fmt.Println(<-messages) } } 

So it really depends on what you know from the tasks you expect.

+5
source

Source: https://habr.com/ru/post/972067/


All Articles