trustmaster / goflow

Flow-based and dataflow programming library for Go (golang)
MIT License
1.6k stars 125 forks source link

How to wait for two inputs? #27

Closed samuell closed 9 years ago

samuell commented 9 years ago

Hello Vlad!

Great to see work going on, on GoFlow! :+1:

I started looking at it again for some recent use cases, but had one question: How would I implement a component that waits for two inputs, before it does its job? The default way seems to be to implement a On<InportName>() function, but so I would need something that does On<Inport1><Inport2>() :) ... but I'm sure there is a better way for this?

Best // Samuel

trustmaster commented 9 years ago

Hi @samuell!

There are basically 2 approaches to this.

The first approach is reactive and queue-based, it requires you to provide both OnInPort1() and OnInPort2() handlers, make a internal buffer to queue incoming data until there is a complete pair. I guess you get the idea.

The second approach is writing a classical active FBP component like this:

// An active Looper component in classical FBP style
type Add2Ports struct {
    Component
    In1  <-chan int
    In2  <-chan int
    Out chan<- int
}

func (c *counter) Loop() {
    for {
        // First read from input one
        in1 := <- c.In1
        // Then read from input two
        in2 := <- c.In2
        // Then do something useful
        c.Out <- in1 + in2
    }
}

The loop is run in its own goroutine which is blocked every time it needs to wait for incoming data on one of the ports. You can also use non-zero connection buffers to get better performance.

samuell commented 9 years ago

Thanks much, @trustmaster for the info! Looks good!

seanward commented 9 years ago

@samuell You can see an example of the internal queueing approach from our antha cross compiler as well. For example: https://github.com/antha-lang/antha/blob/master/reference/reference.go showing the compiler output for an element that allows individual wiring of the inputs, but requires all parameters to have arrived before it fires.

samuell commented 9 years ago

Many thanks @seanward, will have a look!