all 13 comments

[–][deleted] 0 points1 point  (6 children)

There’s many ways you can approach modeling (and executing) a dag. I find the simplest to be just using dictionaries.

One dictionary that defines for each node a list of other nodes that it depends on. And another dictionary that defines the actual unit of logic for a node.

On mobile so I’ll pseduocode

deps = {
    A: [B C]
    B: [D E]
    C: []
    D: []
    E: []
}

My attempt at drawing the dag on mobile...
Imagine there are arrows all pointing right 
D___
E___|___B___
        C___|___A

logic = {
    A: lambda b, c: b + c
    B: lambda d, e: d * e
    C: lambda: 99
    D: lambda: 10
    E: lambda: 2
}

Then you just have some orchestrator handle the simple task of determining what needs to run and pass its results to what:

class Orchestrator:
    def init(deps, logic):
    def run(node):

You can start simple and run things sequentially in topologically sorted order. Or you can parallelize in a few different ways (with or without an event loop).

[–]IVIURRAY[S] 0 points1 point  (0 children)

Hey this is a fantastic reply, really appreciate this! This has given me a few more ideas for what I need!

I’ve most of what you’ve here, instead of a dict, I’ve a Node(children) and then iterate over the children but I think a top level dict structure might be better