Worker

package module
v1.0.1-0...-6a54ea3 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2024 License: MIT Imports: 2 Imported by: 0

README

Worker

Worker, which uses sync.Cond instead of channels.

Installing:

go get -u github.com/rustatian/Worker

Sample of usage:

package main

import (
	"fmt"
	"github.com/rustatian/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
		}
	})
}

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 any)

Add adds a new item to the work queue

func (*Work) Run

func (w *Work) Run(n int, f func(item any))

Jump to

Keyboard shortcuts

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