mirror of https://github.com/panjf2000/ants.git
update some annotations
This commit is contained in:
parent
4237f29584
commit
917dfb037f
24
README.md
24
README.md
|
@ -68,7 +68,7 @@ func main() {
|
||||||
|
|
||||||
runTimes := 1000
|
runTimes := 1000
|
||||||
|
|
||||||
// Uses the common pool
|
// Use the common pool
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -81,14 +81,14 @@ func main() {
|
||||||
fmt.Printf("running goroutines: %d\n", ants.Running())
|
fmt.Printf("running goroutines: %d\n", ants.Running())
|
||||||
fmt.Printf("finish all tasks.\n")
|
fmt.Printf("finish all tasks.\n")
|
||||||
|
|
||||||
// Uses the pool with a function,
|
// Use the pool with a function,
|
||||||
// sets 10 to the size of goroutine pool and 1 second for expired duration
|
// set 10 to the size of goroutine pool and 1 second for expired duration
|
||||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
||||||
myFunc(i)
|
myFunc(i)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
defer p.Release()
|
defer p.Release()
|
||||||
// Submits tasks
|
// Submit tasks
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
p.Serve(int32(i))
|
p.Serve(int32(i))
|
||||||
|
@ -141,7 +141,7 @@ func main() {
|
||||||
|
|
||||||
request := &Request{Param: param, Result: make(chan []byte)}
|
request := &Request{Param: param, Result: make(chan []byte)}
|
||||||
|
|
||||||
// Throttles the requests with ants pool. This process is asynchronous and
|
// Throttle the requests traffic with ants pool. This process is asynchronous and
|
||||||
// you can receive a result from the channel defined outside.
|
// you can receive a result from the channel defined outside.
|
||||||
if err := pool.Serve(request); err != nil {
|
if err := pool.Serve(request); err != nil {
|
||||||
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
||||||
|
@ -154,28 +154,28 @@ func main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Submits tasks
|
## Submit tasks
|
||||||
Tasks can be submitted by calling `ants.Submit(func())`
|
Tasks can be submitted by calling `ants.Submit(func())`
|
||||||
```go
|
```go
|
||||||
ants.Submit(func(){})
|
ants.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
## Customizes limited pool
|
## Customize limited pool
|
||||||
`ants` also supports customizing limited pool. You can use the `NewPool` method to create a pool with the given capacity, as following:
|
`ants` also supports customizing limited pool. You can use the `NewPool` method to create a pool with the given capacity, as following:
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
// Sets 10000 the size of goroutine pool
|
// Set 10000 the size of goroutine pool
|
||||||
p, _ := ants.NewPool(10000)
|
p, _ := ants.NewPool(10000)
|
||||||
// Submits a task
|
// Submit a task
|
||||||
p.Submit(func(){})
|
p.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
## Tunes pool capacity
|
## Tune pool capacity
|
||||||
You can change `ants` pool capacity at any time with `ReSize(int)`:
|
You can change `ants` pool capacity at any time with `ReSize(int)`:
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
pool.ReSize(1000) // Tunes its capacity to 1000
|
pool.ReSize(1000) // Tune its capacity to 1000
|
||||||
pool.ReSize(100000) // Tunes its capacity to 100000
|
pool.ReSize(100000) // Tune its capacity to 100000
|
||||||
```
|
```
|
||||||
|
|
||||||
Don't worry about the synchronous problems in this case, this method is thread-safe.
|
Don't worry about the synchronous problems in this case, this method is thread-safe.
|
||||||
|
|
18
README_ZH.md
18
README_ZH.md
|
@ -67,7 +67,7 @@ func main() {
|
||||||
|
|
||||||
runTimes := 1000
|
runTimes := 1000
|
||||||
|
|
||||||
// Uses the common pool
|
// Use the common pool
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -80,14 +80,14 @@ func main() {
|
||||||
fmt.Printf("running goroutines: %d\n", ants.Running())
|
fmt.Printf("running goroutines: %d\n", ants.Running())
|
||||||
fmt.Printf("finish all tasks.\n")
|
fmt.Printf("finish all tasks.\n")
|
||||||
|
|
||||||
// Uses the pool with a function,
|
// Use the pool with a function,
|
||||||
// sets 10 to the size of goroutine pool and 1 second for expired duration
|
// set 10 to the size of goroutine pool and 1 second for expired duration
|
||||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
||||||
myFunc(i)
|
myFunc(i)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
defer p.Release()
|
defer p.Release()
|
||||||
// Submits tasks
|
// Submit tasks
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
p.Serve(int32(i))
|
p.Serve(int32(i))
|
||||||
|
@ -140,7 +140,7 @@ func main() {
|
||||||
|
|
||||||
request := &Request{Param: param, Result: make(chan []byte)}
|
request := &Request{Param: param, Result: make(chan []byte)}
|
||||||
|
|
||||||
// Throttles the requests with ants pool. This process is asynchronous and
|
// Throttle the requests traffic with ants pool. This process is asynchronous and
|
||||||
// you can receive a result from the channel defined outside.
|
// you can receive a result from the channel defined outside.
|
||||||
if err := pool.Serve(request); err != nil {
|
if err := pool.Serve(request); err != nil {
|
||||||
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
http.Error(w, "throttle limit error", http.StatusInternalServerError)
|
||||||
|
@ -163,9 +163,9 @@ ants.Submit(func(){})
|
||||||
`ants`支持实例化使用者自己的一个 Pool ,指定具体的池容量;通过调用 `NewPool` 方法可以实例化一个新的带有指定容量的 Pool ,如下:
|
`ants`支持实例化使用者自己的一个 Pool ,指定具体的池容量;通过调用 `NewPool` 方法可以实例化一个新的带有指定容量的 Pool ,如下:
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
// Sets 10000 the size of goroutine pool
|
// Set 10000 the size of goroutine pool
|
||||||
p, _ := ants.NewPool(10000)
|
p, _ := ants.NewPool(10000)
|
||||||
// Submits a task
|
// Submit a task
|
||||||
p.Submit(func(){})
|
p.Submit(func(){})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -173,8 +173,8 @@ p.Submit(func(){})
|
||||||
需要动态调整协程池容量可以通过调用`ReSize(int)`:
|
需要动态调整协程池容量可以通过调用`ReSize(int)`:
|
||||||
|
|
||||||
``` go
|
``` go
|
||||||
pool.ReSize(1000) // Tuning its capacity to 1000
|
pool.ReSize(1000) // Tune its capacity to 1000
|
||||||
pool.ReSize(100000) // Tuning its capacity to 100000
|
pool.ReSize(100000) // Tune its capacity to 100000
|
||||||
```
|
```
|
||||||
|
|
||||||
该方法是线程安全的。
|
该方法是线程安全的。
|
||||||
|
|
|
@ -49,7 +49,7 @@ func main() {
|
||||||
|
|
||||||
runTimes := 1000
|
runTimes := 1000
|
||||||
|
|
||||||
// Uses the common pool
|
// Use the common pool
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
@ -62,14 +62,14 @@ func main() {
|
||||||
fmt.Printf("running goroutines: %d\n", ants.Running())
|
fmt.Printf("running goroutines: %d\n", ants.Running())
|
||||||
fmt.Printf("finish all tasks.\n")
|
fmt.Printf("finish all tasks.\n")
|
||||||
|
|
||||||
// Uses the pool with a function,
|
// Use the pool with a function,
|
||||||
// sets 10 to the size of goroutine pool and 1 second for expired duration
|
// set 10 to the size of goroutine pool and 1 second for expired duration
|
||||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) {
|
||||||
myFunc(i)
|
myFunc(i)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
defer p.Release()
|
defer p.Release()
|
||||||
// Submits tasks
|
// Submit tasks
|
||||||
for i := 0; i < runTimes; i++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
p.Serve(int32(i))
|
p.Serve(int32(i))
|
||||||
|
|
Loading…
Reference in New Issue