Skip to content

Commit

Permalink
add workpool
Browse files Browse the repository at this point in the history
  • Loading branch information
谢小军 authored and 谢小军 committed Sep 16, 2019
1 parent ea0628b commit af159c3
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 25 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(10) //Set the maximum number of threads,设置最大线程数
wp := workpool.New(10) //Set the maximum number of threads,设置最大线程数
for i := 0; i < 20; i++ { //开启20个请求
ii := i
wp.Do(func() error {
Expand All @@ -47,11 +47,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(10) //Set the maximum number of threads,设置最大线程数
wp := workpool.New(10) //Set the maximum number of threads,设置最大线程数
for i := 0; i < 20; i++ {
ii := i
wp.Do(func() error {
Expand Down Expand Up @@ -84,11 +84,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(5) //Set the maximum number of threads,设置最大线程数
wp := workpool.New(5) //Set the maximum number of threads,设置最大线程数
for i := 0; i < 10; i++ {
// ii := i
wp.Do(func() error {
Expand Down Expand Up @@ -116,11 +116,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(5) //Set the maximum number of threads,设置最大线程数
wp := workpool.New(5) //Set the maximum number of threads,设置最大线程数
for i := 0; i < 10; i++ {
ii := i
wp.DoWait(func() error {
Expand Down
16 changes: 8 additions & 8 deletions README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(10) //设置最大线程数
wp := workpool.New(10) //设置最大线程数
for i := 0; i < 20; i++ { //开启20个请求
ii := i
wp.Do(func() error {
Expand All @@ -45,11 +45,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(10) //设置最大线程数
wp := workpool.New(10) //设置最大线程数
for i := 0; i < 20; i++ { //开启20个请求
ii := i
wp.Do(func() error {
Expand Down Expand Up @@ -82,11 +82,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(5) //设置最大线程数
wp := workpool.New(5) //设置最大线程数
for i := 0; i < 10; i++ { //开启20个请求
// ii := i
wp.Do(func() error {
Expand Down Expand Up @@ -114,11 +114,11 @@ import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workerpool.New(5) //设置最大线程数
wp := workpool.New(5) //设置最大线程数
for i := 0; i < 10; i++ { //开启20个请求
ii := i
wp.DoWait(func() error {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package main
import (
"fmt"

"github.com/xxjwxc/gowp/workerpool"
"github.com/xxjwxc/gowp/workpool"
"github.com/xxjwxc/public/errors"
)

func tets() {
wp := workerpool.New(5) //设置最大线程数
wp := workpool.New(5) //设置最大线程数
wp.DoWait(func() error {
for j := 0; j < 10; j++ {
fmt.Println(fmt.Sprintf("%v->\t%v", 000, j))
Expand Down
2 changes: 1 addition & 1 deletion workerpool/def.go → workpool/def.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package workerpool
package workpool

import (
"sync"
Expand Down
8 changes: 3 additions & 5 deletions workerpool/workerpool.go → workpool/workpool.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package workerpool
package workpool

import (
"context"
"sync/atomic"
"time"

"github.com/xxjwxc/public/myqueue"

"github.com/xxjwxc/public/mylog"
)

//New 注册工作池,并设置最大并发数
Expand Down Expand Up @@ -127,7 +125,7 @@ func (p *WorkerPool) loop(maxWorkersCount int) {
case <-ct.Done():
p.errChan <- ct.Err()
//if atomic.LoadInt32(&p.closed) != 1 {
mylog.Error(ct.Err())
//mylog.Error(ct.Err())
atomic.StoreInt32(&p.closed, 1)
cancel()
case <-closed:
Expand All @@ -141,7 +139,7 @@ func (p *WorkerPool) loop(maxWorkersCount int) {
select {
case p.errChan <- err:
//if atomic.LoadInt32(&p.closed) != 1 {
mylog.Error(err)
//mylog.Error(err)
atomic.StoreInt32(&p.closed, 1)
default:
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package workerpool
package workpool

import (
"fmt"
Expand Down

0 comments on commit af159c3

Please sign in to comment.