Git Product home page Git Product logo

parwork's Introduction

parwork Build Status codecov Go Report Card GoDoc

Parallel work processing package

Description

This package allows work to processed in parallel using a fork-join pattern. The implementation relies on goroutines, channels and wait groups.

The implementation allows the configuration of the processor by providing the degree of parallelism which defines how many goroutines will process work from the queues in parallel. By creating only a small number of goroutines, which defaults to the number of cores on the system, we avoid context switching instead of allowing a high number of goroutines to fight for processor resources.

This is the reason why this package makes more sense when used with work items that are CPU bound and do not switch context like waiting for IO.

In order to use the package the user has only to provide the implementation of the following:

Work interface

type Work interface {
    Do()
    GetError() error
    Result() interface{}
}

The work interface defines a method Do() which contains all the processing logic of the work item. The GetError() error method can be used to flag the work item as failed and return a error. The Result() interface{} defines a method which returns the result of the work. Due to the lack of generics the data return has to be cast from interface{} to the actual result type in order to be usable in the WorkCollector.

WorkGenerator function

type WorkGenerator func() Work

The WorkGenerator function allows the user to provide a implementation that returns on each call a work item to be processed. If the generator returns nil the generation of work has finished.

WorkCollector function

type WorkCollector func(Work)

The WorkCollector function takes as a argument a completed Work item. It can check for a failure by calling the GetError or the Result method of the Work item and handle it appropriately.

Example

For a example implementation please take a look in the examples folder of the repository. The example implements a brute force method of trying to find the MD5 hash of a string. This is just a example implementation to demonstrate the usage of the package. And it should not be misused to break secrets.

parwork's People

Contributors

mantzas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

parwork's Issues

parwork: idiomatic Work interface

hi,

just a drive-by comment...
I wouldn't define the Work interface like:

type Work interface {
	Do()
	GetError() error
	Result() interface{}
}

but, instead:

type Work interface {
	Do()
	Err() error
	Result() interface{}
}

to piggy-back on other well known APIs in Go (context.Context, bufio.Scanner, ...)

Profile and create examples

Create an example with simple processing in order to comapre:

  • Runniing to end with parwork and with simple implementation
  • Trace both and provide profile

Stop processing on demand

The process will run until the Work Generator will return nil marking the end of the processing work.
There are several reasons why the process might end earlier:

  • The collector checks for a error and stops the process
  • The work contains the needed result and does not need to continue
  • The user cancels the processing

For the above and other reasons we need a way to stop the processing pipeline.

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.