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

Add Submit() API for submitting async tasks. There are good reasons for asynchronous APIs to come back. #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

piaodazhu
Copy link

I checked the issue and it seems that async APIs is provided in the early version but now have been deprecated. In this PR I want to resume the async APIs and give out my reason.

pool.Process() is a sync API, and it will not return until the task is finished. Sometimes we need to submit async tasks. For running async tasks, one simple way is: (see issue #9)

go func() {
    foo := pool.Process(MyTask)
}()

However, the above method cannot control the number growth of goroutines. If there are 4 workers but 10000 tasks, the above method will create 10000 goroutines. But we wish these are only 4 worker goroutines running these tasks. In this case, pool.Submit() is a better choice:

pool.Submit(func() {
	// do your task
})

The source code of tunny is short and elegant, but I think async APIs are necessary for a Go task pool module. Similar effects can be achieved by some indirect methods, but tunny has already provided the framework to do this in a more efficient way. So I actually only add a new API and a new Worker implementation for it, without changing the current APIs.

@natpacket
Copy link

If the task is larger than the number of workers, it will also block😊

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

Successfully merging this pull request may close these issues.

2 participants