GithubHelp home page GithubHelp logo

oliveo / work Goto Github PK

View Code? Open in Web Editor NEW

This project forked from goinggo/work

0.0 2.0 0.0 206 KB

Implements a dynamic work pool of goroutines to perform tasks.

License: MIT License

Go 100.00%

work's Introduction

Work

Copyright 2014 Ardan Studios. All rights reserved.
Use of this source code is governed by a MIT license that can be found in the LICENSE handle.

Package work uses an unbuffered channel to create a pool of goroutines that can perform work. Stats are maintained on the number of pending and active requests in the pool. Goroutines can be added and removed from the pool during pool operation. This allows for the implementation of a control port to adjust the size of the pool for maximum performance.

Ardan Studios
12973 SW 112 ST, Suite 153
Miami, FL 33186
[email protected]

Click To View Documentation

Sample App

// This sample program demostrates how to use the work package
// to use a pool of goroutines to get work done.
package main

import (
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/goinggo/work"
)

// names provides a set of names to display.
var names = []string{
	"steve",
	"bob",
	"mary",
	"therese",
	"jason",
}

// namePrinter provides special support for printing names.
type namePrinter struct {
	name string
}

// Work implements the Worker interface.
func (m *namePrinter) Work(id int) {
	fmt.Println(m.name)
	time.Sleep(time.Second)
}

func logFunc(message string) {
	log.Println(message)
}

// main is the entry point for all Go programs.
func main() {
	// Create a work value with 2 goroutines.
	w, err := work.New(2, time.Second, logFunc)
	if err != nil {
		log.Fatalln(err)
	}

	var wg sync.WaitGroup
	wg.Add(10 * len(names))

	for i := 0; i < 10; i++ {
		// Iterate over the slice of names.
		for _, name := range names {
			// Create a namePrinter and provide the
			// specfic name.
			np := namePrinter{
				name: name,
			}

			go func() {
				// Submit the task to be worked on. When Run
				// returns we know it is being handled.
				w.Run(&np)
				wg.Done()
			}()
		}
	}

	for {
		// Enter a number and hit enter to change the size
		// of the work pool.
		var c int
		fmt.Scanf("%d", &c)
		if c == 0 {
			break
		}

		w.Add(c)
	}

	wg.Wait()

	// Shutdown the work and wait for all existing work
	// to be completed.
	w.Shutdown()
}

work's People

Contributors

ardan-bkennedy avatar goinggo avatar

Watchers

James Cloos avatar Woodpecker avatar

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.