Closed cuishuang closed 1 week ago
现在已经有了:https://pkg.go.dev/github.com/panjf2000/ants/v2#Pool.ReleaseTimeout
恩,ReleaseWaitAllWorkersExit()就是参照的这个ReleaseTimeout;但ReleaseTimeout有两个问题:1是依然需要传入一个timeout的入参,无法做到准确的结束所有worker后就返回;2是没有在ants.go里封装一个可对外暴露的方法,defaultAntsPool无法直接调用
现在已经有了:https://pkg.go.dev/github.com/panjf2000/ants/v2#Pool.ReleaseTimeout
确实有这个需求,需要等待所有正在执行的协程都退出了,主进程才退出,目前只能猜测可能的退出时间.
需求+1 比如需要等到所有协程执行结果后再往下执行 最好设置个通道 等所有协程执行结束后通知
需求+1 比如需要等到所有协程执行结果后再往下执行 最好设置个通道 等所有协程执行结束后通知
我都忘了咋回事了(捂脸)
This issue is marked as stale because it has been open for 30 days with no activity.
You should take one of the following actions:
This issue will be automatically closed in 7 days if no further activity occurs.
This issue was closed because it has been inactive for 7 days since being marked as stale.
If you believe this is a false alarm, please leave a comment for it or open a new issue, you can also reopen this issue directly if you have permission.
由于ants中var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize),会在使用到该库的项目初始化时,先于项目的main.go执行,从而会多出一个协程;
很多项目单测中集成了go.uber.org/goleak,用于检测goroutine泄露,从而会导致测试失败,如下图:
查看ants代码,发现有一个Release()方法,可以释放掉worker,但释放需要一定时间,只能休眠一个足够长的时间(经验值是1s左右)
建议新增一个 ReleaseWaitAllWorkersExit()方法,调用后,确保所有worker都释放后才返回
另外几个可能相关的issue:
https://github.com/panjf2000/ants/issues/212
https://github.com/panjf2000/ants/issues/225
related pr:https://github.com/panjf2000/ants/pull/245