Example #1
0
// Calls fn with each item received from inputs, and outputs the results in the same order to the returned channel.
func (me *Executor) Map(fn func(interface{}) interface{}, inputs <-chan interface{}) <-chan interface{} {
	ret := make(chan interface{})
	go func() {
		futs := queue.New()
		go func() {
			for {
				_fut, ok := futs.Get()
				if !ok {
					break
				}
				ret <- _fut.(*Future).Result()
			}
			close(ret)
		}()
		for a := range inputs {
			fut := me.Submit(func(a interface{}) func() interface{} {
				return func() interface{} {
					return fn(a)
				}
			}(a))
			futs.Put(fut)
		}
		futs.Close()
	}()
	return ret
}
Example #2
0
// Create a new Executor that does up to maxWorkers tasks in parallel.
func NewExecutor(maxWorkers int) *Executor {
	ret := &Executor{
		waiting: queue.New(),
	}
	for a := 0; a < maxWorkers; a++ {
		go func() {
			for {
				val, ok := ret.waiting.Get()
				if !ok {
					return
				}
				val.(*Future).run()
			}
		}()
	}
	return ret
}