Worker

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2018 License: MIT Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Worker

Worker uses sync.Cond instead of channels.

Installing: go get -u github.com/ValeryPiashchynski/Worker

Sample of usage:

package main

import (
	"fmt"
	"github.com/ValeryPiashchynski/Worker"
	"io/ioutil"
	"net/http"
	"runtime"
)

func main() {
	// create a variable
	var w Worker.Work
	
	// example of sites to get info from in parallel
	sites := []string{"http://google.com", "http://amazon.com", "http://spiralscout.com", "http://0xdev.me"}

    // add this sites to worker
	for _, v := range sites {
		w.Add(v)
	}

    // Run the work in 10 goroutines (for example)
    // So, we know, that we working with strings, and we need to make type assertion
    // Each task will be handled by separate goroutine
	w.Run(10, func(item interface{}) {
		str := item.(string)

        // we also could add work during the process of running
		w.Add("http://")
		r, err := http.Get(str)
		if err != nil {
			fmt.Println(err)
			return
		}

		_, err = ioutil.ReadAll(r.Body)
		if err != nil {
			fmt.Println(err)
			return
		}
	})
}

Valery Piashchynski

https://SpiralScout.com

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Work

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

func (*Work) Add

func (w *Work) Add(item interface{})

func (*Work) Run

func (w *Work) Run(n int, f func(item interface{}))

Jump to

Keyboard shortcuts

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