parallel

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 2 Imported by: 0

README

parallel

The parallel library provides a simple way to manage concurrent execution of tasks using a pool of goroutines with a specified limit. This allows you to control and limit the number of concurrent goroutines in your Go applications.

Features

  • Create a pool of goroutines with a maximum concurrency limit.
  • Add tasks to the pool with concurrency control.
  • Wait for all tasks to complete.

Installation

To use the parallel library in your Go project, simply import parallel package into your project.

go get github.com/gohacks/parallel

Usage

Example

Here's a basic example of how to use the parallel library:

package main

import (
	"context"
	"fmt"
	"time"
	"github.com/gohacks/parallel"
)

func main() {
	// Create a new pool with a maximum of 3 concurrent goroutines
	// with not stop in case of error
	pool := parallel.New(context.Background(), 3, false)

	// Add tasks to the pool
	for i := 1; i <= 10; i++ {
		pool.Run(func() {
			yourFunction(yourParams) // call your functions here
		})
	}

	// Wait for all tasks to complete
	pool.Wait()

	if len(parallel.Errors()) > 0 {
		// Do something in case of error
	}
}

API

New(ctx context.Context, maxGoroutines int, cancelOnError bool) *Parallel Creates a new instance of Parallel with the specified limit of goroutines.

maxGoroutines: The maximum number of goroutines that can run concurrently. (*Parallel) Run(task func()) Adds a new task to be executed in the parallel with concurrency control.

task: A function to be executed by the parallel. (*Parallel) Wait() Waits for all tasks to complete.

Notes

  • The library uses a semaphore pattern with channels to manage concurrency.
  • Ensure that the maxGoroutines value is set according to the resources available and the requirements of your application.

Contributing

Contributions are welcome! If you have any improvements or bug fixes, please open an issue or submit a pull request.

License

This library is released under the MIT License. See the LICENSE file for more details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parallel

type Parallel struct {
	// contains filtered or unexported fields
}

func New

func New(ctx context.Context, maxGoroutines int, cancelOnError bool) *Parallel

New creates a new instance of Pool with the specified limit of goroutines.

func (*Parallel) Errors added in v1.0.0

func (p *Parallel) Errors() []error

func (*Parallel) Run

func (p *Parallel) Run(task func() error)

Run adds a new function to be executed in the parallel with concurrency control.

func (*Parallel) Wait

func (p *Parallel) Wait()

Wait aguarda que todas as goroutines terminem.

Jump to

Keyboard shortcuts

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