Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提议:新增 ReleaseWaitAllWorkersExit(),用于等待所有worker都被释放后再返回 #244

Open
cuishuang opened this issue Aug 5, 2022 · 5 comments
Assignees
Labels
proposal Proposal for this repo

Comments

@cuishuang
Copy link

cuishuang commented Aug 5, 2022

由于ants中var defaultAntsPool, _ = NewPool(DefaultAntsPoolSize),会在使用到该库的项目初始化时,先于项目的main.go执行,从而会多出一个协程;

很多项目单测中集成了go.uber.org/goleak,用于检测goroutine泄露,从而会导致测试失败,如下图:

image

查看ants代码,发现有一个Release()方法,可以释放掉worker,但释放需要一定时间,只能休眠一个足够长的时间(经验值是1s左右)

image

image

建议新增一个 ReleaseWaitAllWorkersExit()方法,调用后,确保所有worker都释放后才返回

image

另外几个可能相关的issue:

#212

#225


related pr:#245

@panjf2000
Copy link
Owner

@cuishuang
Copy link
Author

现在已经有了:https://pkg.go.dev/github.com/panjf2000/ants/v2#Pool.ReleaseTimeout

恩,ReleaseWaitAllWorkersExit()就是参照的这个ReleaseTimeout;但ReleaseTimeout有两个问题:1是依然需要传入一个timeout的入参,无法做到准确的结束所有worker后就返回;2是没有在ants.go里封装一个可对外暴露的方法,defaultAntsPool无法直接调用

@lifei6671
Copy link

现在已经有了:https://pkg.go.dev/github.com/panjf2000/ants/v2#Pool.ReleaseTimeout

确实有这个需求,需要等待所有正在执行的协程都退出了,主进程才退出,目前只能猜测可能的退出时间.

@HaiboLee
Copy link

需求+1 比如需要等到所有协程执行结果后再往下执行 最好设置个通道 等所有协程执行结束后通知

@cuishuang
Copy link
Author

需求+1 比如需要等到所有协程执行结果后再往下执行 最好设置个通道 等所有协程执行结束后通知

我都忘了咋回事了(捂脸)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal Proposal for this repo
Projects
None yet
Development

No branches or pull requests

4 participants