Skip to content

Commit

Permalink
update timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
xxjwxc committed Jan 2, 2020
1 parent 891db1e commit b08b68b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
- Concurrency limiting goroutine pool.
- Limits the concurrency of task execution, not the number of tasks queued.
- Never blocks submitting tasks, no matter how many tasks are queued.
- Support timeout
- Support through security queues [queue](https://github.com/xxjwxc/public/tree/master/myqueue)

- golang workpool common library

## Installation

Expand Down Expand Up @@ -140,7 +140,7 @@ import (
)
func main() {
wp := workpool.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 All @@ -165,3 +165,38 @@ func main() {
fmt.Println("down")
}
```

### Support timeout exit

```
package main
import (
"fmt"
"time"
"time"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workpool.New(5) // Set the maximum number of threads
wp.SetTimeout(time.Millisecond) // set max timeout
for i := 0; i < 10; i++ {
ii := i
wp.DoWait(func() error {
for j := 0; j < 5; j++ {
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
time.Sleep(1 * time.Second)
}
return nil
})
}
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}
```
37 changes: 36 additions & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- 无论排队多少任务,都不会阻止提交任务。
- 通过队列支持[queue](https://github.com/xxjwxc/public/tree/master/myqueue)

- golang 工作池公共库
### golang 工作池公共库

## 安装

Expand Down Expand Up @@ -159,3 +159,38 @@ func main() {
fmt.Println("down")
}
```

### 支持超时退出

```
package main
import (
"fmt"
"time"
"time"
"github.com/xxjwxc/gowp/workpool"
)
func main() {
wp := workpool.New(5) // 设置最大线程数
wp.SetTimeout(time.Millisecond) // 设置超时时间
for i := 0; i < 10; i++ { // 开启20个请求
ii := i
wp.DoWait(func() error {
for j := 0; j < 5; j++ {
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
time.Sleep(1 * time.Second)
}
return nil
})
}
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}
```
5 changes: 4 additions & 1 deletion workpool/workpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func TestWorkerPoolStart(t *testing.T) {
})
}

wp.Wait()
err := wp.Wait()
if err != nil {
fmt.Println(err)
}
fmt.Println("down")
}

Expand Down

0 comments on commit b08b68b

Please sign in to comment.