Git Product home page Git Product logo

concurrently's Introduction

concurrently

Simple, easy concurrent processing in Go

Overview

concurrently makes it easy to concurrently process collections using higher-order functions like Map and Filter.

For example, if you have a urls array and a getHttpResponse function, you can distribute the requests across 16 goroutines with a single line:

responses := concurrently.Map(urls, getHttpResponse, 16).([]http.Response)

You could also concurrently filter a collection across 64 goroutines:

activeUsers := concurrently.Filter(users, isUserActive, 64).([]User)

Or call a function without requiring a return value:

concurrently.Each(images, resizeImage, 16)

For IO-bound operations, the execution time will be inversely proportional to the number of goroutines. If an operation averages 117 ms with 1 goroutine, it might average 11.8 ms with 10 goroutines or 1.51 ms with 100 goroutines:

BenchmarkMap001       20   117447904 ns/op
BenchmarkMap010      100   11803551 ns/op
BenchmarkMap100     2000   1511264 ns/op
BenchmarkMapSerial    20   116438290 ns/op

Usage

For full documentation, see the GoDoc documentation.

Map

For example, to square every int in a slice of ints:

// Create a slice of ints
numbers := make([]int, 10)
for i := range numbers {
  numbers[i] = i
}

// Define the function
func square(number int) int {
  return number * number
}

// Perform the mapping, distributing it across 8 goroutines
squaredNumbers := concurrently.Map(numbers, square, 8).([]int)

The function should have one parameter (of any type) and one return value (of any type, not necessarily the same type as the parameter).

Filter

For example, to retrieve the even ints from a slice of ints:

// Create a slice of ints
numbers := make([]int, 10)
for i := range numbers {
  numbers[i] = i
}

// Define the function
func isEvenNumber(number int) bool {
  return int(math.Mod(float64(number), float64(2))) == 0
}

// Perform the filtering, distributing it across 8 goroutines
evenNumbers := concurrently.Filter(numbers, isEvenNumber, 8).([]int)

The function should have one parameter (of any type) and return a bool.

Each

For example, to log a number of messages:

// Create an array of messages
messages := []string{"a", "b", "c", "d", "e", "f", "g", "h"}

// Define the function
func logMessage(message string) {
  log.Print(message)
}

// Log the messages
concurrently.Each(messages, logMessage, 4)

License

concurrently is released under the MIT License. Please see the MIT-LICENSE file for details.

concurrently's People

Contributors

tombenner avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

luomeiqin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.