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

how to pass context and other args in pool_func? #81

Open
kungf opened this issue Mar 18, 2020 · 12 comments
Open

how to pass context and other args in pool_func? #81

kungf opened this issue Mar 18, 2020 · 12 comments
Assignees
Labels
help wanted Extra attention is needed question Further information is requested waiting for response waiting for the response from commenter

Comments

@kungf
Copy link

kungf commented Mar 18, 2020

hello, @panjf2000
func NewPoolWithFunc(size int, pf func(interface{}), options ...Option)
in NewPoolWithFunc, pf only have an ingerface{}, but i want to pass at least two args, how to solve tihes?

my run function like :

func (s *fileSystemServer) handleOp(
	c *Connection,
	ctx context.Context,
	op interface{}) {
}

context is not suggested to put into a struct, how to pass ctx + args as one task to queue?

@kungf kungf added help wanted Extra attention is needed question Further information is requested labels Mar 18, 2020
@panjf2000
Copy link
Owner

You can define a struct which contains all your args and use this struct as the single arg of pf, then restore it to struct from an interface in pf and you can get all args.

@kungf

@panjf2000 panjf2000 added the waiting for response waiting for the response from commenter label Mar 18, 2020
@kungf
Copy link
Author

kungf commented Mar 18, 2020

yes, this is what i first want resolution, but the context arg was not suggested to put into struct, so a little complex
https://groups.google.com/forum/#!topic/golang-nuts/xRbzq8yzKWI

@panjf2000

@panjf2000
Copy link
Owner

Then I'm out of solutions, maybe I should start considering refactor pool_func and make it accept variable arguments like func NewPoolWithFunc(size int, pf func(...interface{}), options ...Option)

@davidxiaozhi
Copy link

davidxiaozhi commented Aug 25, 2020

@panjf2000 could use this method?

for j := 0; j < batchCount; j++ {
		bj:=j
		routinePoll.Submit(func() {
			genBucketBatch(bj)
		})
 }

@panjf2000
Copy link
Owner

I don't quite get your point, what did you want to ask exactly? Is your question related to this issue? if not, please open your own issue.

@davidxiaozhi

@robinx2012
Copy link

@panjf2000 could use this method?

for j := 0; j < batchCount; j++ {
		bj:=j
		routinePoll.Submit(func() {
			genBucketBatch(bj)
		})
 }

that works for me, thk~

@mohammad-babaei
Copy link

Then I'm out of solutions, maybe I should start considering refactor pool_func and make it accept variable arguments like func NewPoolWithFunc(size int, pf func(...interface{}), options ...Option)

I have the same problem with args and it would be better if it was ...interface{} instead of interface{}.
Are you gonna fix it?
If not i will have to look for an alternative.
thank you in advance

@rohmanhm
Copy link

@panjf2000 could use this method?

for j := 0; j < batchCount; j++ {
		bj:=j
		routinePoll.Submit(func() {
			genBucketBatch(bj)
		})
 }

Thank you so much! This works for me without creating a new pool with function.

@Reid00
Copy link

Reid00 commented Nov 22, 2022

same issue, how to pass multi args of task, is there any good suggestion?

@tong3jie
Copy link

tong3jie commented Aug 9, 2023

同样的问题,如何传递任务的多个参数,有什么好的建议吗?

func A(a string, b string) func() {
	return func() {
		fmt.Println(a, b)
	}
}
f := A("a","b")
pool.Submit(f)

@someview
Copy link

这个问题有进展吗,字节有一个简短的任务池实现,里面也支持ctx。可以提交包含ctx的任务,还是非常重要的,submit(ctx, func())

@panjf2000
Copy link
Owner

这个问题有进展吗,字节有一个简短的任务池实现,里面也支持ctx。可以提交包含ctx的任务,还是非常重要的,submit(ctx, func())

传入 context 后,是在用户代码里自己使用还是说要在 ants 内部用这个 ctx 控制流程,这个区别很大。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested waiting for response waiting for the response from commenter
Projects
None yet
Development

No branches or pull requests

9 participants