šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

github.com/jake-dog/goroutines

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/jake-dog/goroutines

v0.1.0
Source
Go
Version published
Created
Source

goroutines

Concurrency package emulating a tiny subset of Python's threading module. Implements safe and simple concurrency patterns with abort-on-error and cancellation. Provides simple syntax with golang generics, using only the standard library.

GoDoc Go Report Card License

Key features

  • Concurrent context/error-aware generic map, inject/reduce, and more
  • Reentrant generic function result cache and serializer
  • Timed mutex implementation

Installation

Use go get to install this library:

$ go get github.com/jake-dog/goroutines

Usage example

package main

import (
	"fmt"
	"github.com/jake-dog/goroutines"
)

func main() {
	data := []string{"one", "two", "three", "four", "five", "six", "seven"}

	results := goroutines.Map(2, func(s string) int {
		fmt.Println("Processing:", s)
		return len(s)
	}, data)

	var sum int
	for r := range results { // All results must be consumed
		sum += r
	})
	fmt.Println("Sum of all strings processed:", sum)
}

Performance notes

The size of the goroutine pool provided to Map, Inject, Reduce, Search, etc. also limits the internal buffer size. Unordered functions require less buffering, and are frequently much faster, as the results do not need to be reordered.

Unless a context is provided, error aware mapping functions can only terminate in-between processing of "slow" functions. Context cancellation is preferred to terminate quickly. Error aware mapping functions will return the first error encountered as the result of a function call or context cancellation.

FAQs

Package last updated on 16 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts