Skip to content

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

Open
piaodazhu wants to merge 1 commit into
Jeffail:masterfrom
piaodazhu:master
Open

piaodazhu wants to merge 1 commit into
Jeffail:masterfrom
piaodazhu:master

Conversation

@piaodazhu

Copy link
Copy Markdown

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
Copy Markdown

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

2 participants