Add example in README
This commit is contained in:
parent
5d503a8465
commit
e5b62ddd3f
1 changed files with 53 additions and 1 deletions
54
README.md
54
README.md
|
@ -20,10 +20,62 @@ Priority pool:
|
|||
|
||||
## Install
|
||||
|
||||
```powershell
|
||||
```
|
||||
go get -u github.com/alexvanin/priopool
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/alexvanin/priopool"
|
||||
)
|
||||
|
||||
func main() {
|
||||
regularJob := func(i int) {
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Printf("Job %d is done\n", i)
|
||||
}
|
||||
|
||||
highPriorityJob := func() {
|
||||
fmt.Println("High priority job is done")
|
||||
}
|
||||
|
||||
pool, err := priopool.New(2, -1) // pool for two parallel executions
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
wg.Add(5 + 1)
|
||||
for i := 0; i < 5; i++ {
|
||||
ind := i + 1
|
||||
// enqueue 5 regular jobs
|
||||
pool.Submit(1, func() { regularJob(ind); wg.Done() })
|
||||
}
|
||||
// after 5 regular jobs enqueue high priority job
|
||||
pool.Submit(10, func() { highPriorityJob(); wg.Done() })
|
||||
wg.Wait()
|
||||
|
||||
/*
|
||||
Output:
|
||||
Job 2 is done
|
||||
Job 1 is done
|
||||
High priority job is done
|
||||
Job 4 is done
|
||||
Job 3 is done
|
||||
Job 5 is done
|
||||
*/
|
||||
}
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
Source code is available under the [MIT License](/LICENSE).
|
||||
|
|
Loading…
Reference in a new issue