README.md
<b>A goroutine pool for Go</b>
<a title="Build Status" target="_blank" href="https://github.com/panjf2000/ants/actions?query=workflow%3ATests"></a> <a title="Codecov" target="_blank" href="https://codecov.io/gh/panjf2000/ants"></a> <a title="Release" target="_blank" href="https://github.com/panjf2000/ants/releases"></a> <a title="Tag" target="_blank" href="https://github.com/panjf2000/ants/tags"></a>
<a title="Minimum Go Version" target="_blank" href="https://github.com/panjf2000/gnet"></a> <a title="Go Report Card" target="_blank" href="https://goreportcard.com/report/github.com/panjf2000/ants"></a> <a title="Doc for ants" target="_blank" href="https://pkg.go.dev/github.com/panjf2000/ants/v2?tab=doc"></a> <a title="Mentioned in Awesome Go" target="_blank" href="https://github.com/avelino/awesome-go#goroutines"></a>
</p>English | 中文
Library ants implements a goroutine pool with fixed capacity, managing and recycling a massive number of goroutines, allowing developers to limit the number of goroutines in your concurrent programs.
ants worksants v1go get -u github.com/panjf2000/ants
ants v2 (with GO111MODULE=on)go get -u github.com/panjf2000/ants/v2
Check out the examples for basic usage.
ants.Optionscontains all optional configurations of the ants pool, which allows you to customize the goroutine pool by invoking option functions to set up each configuration in NewPool/NewPoolWithFunc/NewPoolWithFuncGeneric method.
Check out ants.Options and ants.Option for more details.
ants supports customizing the capacity of the pool. You can call the NewPool method to instantiate a Pool with a given capacity, as follows:
p, _ := ants.NewPool(10000)
Tasks can be submitted by calling ants.Submit
ants.Submit(func(){})
You can tune the capacity of ants pool at runtime with ants.Tune:
pool.Tune(1000) // Tune its capacity to 1000
pool.Tune(100000) // Tune its capacity to 100000
Don't worry about the contention problems in this case, the method here is thread-safe (or should be called goroutine-safe).
ants allows you to pre-allocate the memory of the goroutine queue in the pool, which may get a performance enhancement under some special certain circumstances such as the scenario that requires a pool with ultra-large capacity, meanwhile, each task in goroutine lasts for a long time, in this case, pre-mallocing will reduce a lot of memory allocation in goroutine queue.
// ants will pre-malloc the whole capacity of pool when calling ants.NewPool.
p, _ := ants.NewPool(100000, ants.WithPreAlloc(true))
pool.Release()
or
pool.ReleaseTimeout(time.Second * 3)
// A pool that has been released can be still used after calling the Reboot().
pool.Reboot()
All tasks submitted to ants pool will not be guaranteed to be addressed in order, because those tasks scatter among a series of concurrent workers, thus those tasks would be executed concurrently.
Please read our Contributing Guidelines before opening a PR and thank you to all the developers who already made contributions to ants!
The source code in ants is available under the MIT License.
Trusted by the following corporations/organizations.
<table> <tbody> <tr> <td align="center" valign="middle"> <a href="https://www.tencent.com/"> </a>
</td>
<td align="center" valign="middle">
<a href="https://www.bytedance.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://tieba.baidu.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://weibo.com/" target="_blank">
</a>
</td>
</tr>
<tr>
<td align="center" valign="middle">
<a href="https://www.tencentmusic.com/en-us/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.futuhk.com/en/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.shopify.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.wechat.com/en/" target="_blank">
</a>
</td>
</tr>
<tr>
<td align="center" valign="middle">
<a href="https://www.baidu.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.360.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.huaweicloud.com/intl/en-us/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.matrixorigin.io/" target="_blank">
</a>
</td>
</tr>
<tr>
<td align="center" valign="middle">
<a href="https://adguard-dns.io/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://bk.tencent.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.alibabacloud.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.zuoyebang.com/" target="_blank">
</a>
</td>
</tr>
<tr>
<td align="center" valign="middle">
<a href="https://www.antgroup.com/en/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://zilliz.com/" target="_blank">
</a>
</td>
</td>
<td align="center" valign="middle">
<a href="https://amap.com/" target="_blank">
</a>
</td>
<td align="center" valign="middle">
<a href="https://www.apache.org/" target="_blank">
</a>
</td>
</tr>
<tr>
<td align="center" valign="middle">
<a href="https://www.coze.com/" target="_blank">
</a>
</td>
</tr>
If you're also using ants in production, please help us enrich this list by opening a pull request.
The open-source projects below do concurrent programming with the help of ants.
If you have ants integrated into projects, feel free to open a pull request refreshing this list of use cases.
ants has been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.
<a href="https://www.jetbrains.com/?from=ants" target="_blank"></a>
<table> <tbody> <tr> <td align="center" valign="middle"> <a target="_blank" href="https://buymeacoffee.com/panjf2000">Please be sure to leave your name, GitHub account, or other social media accounts when you donate by the following means so that I can add it to the list of donors as a token of my appreciation.
</a>
</td>
<td align="center" valign="middle">
<a target="_blank" href="https://www.patreon.com/panjf2000">
</a>
</td>
<td align="center" valign="middle">
<a target="_blank" href="https://opencollective.com/panjf2000">
</a>
</td>
</tr>