cirque

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 1 Imported by: 1

README

Cirque

A circular queue that processes jobs in parallel but returns results in FIFO.

inputs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

inputChannel, outputChannel := NewCirque(3, func(i int) int {
    time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond)
    return i * 2
})

go func() {
    for _, i := range inputs {
        inputChannel <- i
    }
    close(inputChannel)
}()

var output []int
for i := range outputChannel {
    output = append(output, i)
}
fmt.Println(output)

// Output: [2 4 6 8 10 12 14 16 18 20]

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCirque

func NewCirque[I any, O any](parallelism int64, processor func(I) O) (chan<- I, <-chan O)

NewCirque creates a FIFO parallel queue that runs a given processor function on each job, similar to a parallel Map.

The method accepts a parallelism number, which the maximum number of jobs that are processed simultaneously, and a processor function that takes an input and returns an output. The processor function must be safe to call from multiple goroutines.

It returns two channels, one into which inputs can be passed, and one from which outputs can be read. Closing the input channel will close the output channel after processing is complete. Do not close the output channel yourself.

Example
inputs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

inputChannel, outputChannel := NewCirque(3, func(i int) int {
	time.Sleep(time.Duration(rand.Int63n(100)) * time.Millisecond)
	return i * 2
})

go func() {
	for _, i := range inputs {
		inputChannel <- i
	}
	close(inputChannel)
}()

var output []int
for i := range outputChannel {
	output = append(output, i)
}
fmt.Println(output)
Output:

[2 4 6 8 10 12 14 16 18 20]

Types

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func (*SyncMap[K, V]) Delete

func (tm *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) Get

func (tm *SyncMap[K, V]) Get(key K) (value V, ok bool)

func (*SyncMap[K, V]) Set

func (tm *SyncMap[K, V]) Set(key K, value V)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL