Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Thread pool
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== In languages == In [[Bash (Unix shell)|bash]] implemented by <code>--max-procs</code> / <code>-P</code> in [[xargs]], for example: <syntaxhighlight lang="bash"> # Fetch 5 URLs in parallel urls=( "https://example.com/file1.txt" "https://example.com/file2.txt" "https://example.com/file3.txt" "https://example.com/file4.txt" "https://example.com/file5.txt" ) printf '%s\n' "${urls[@]}" | xargs -P 5 -I {} curl -sI {} | grep -i "content-length:" </syntaxhighlight><ref>{{Cite web |last=Shved |first=Paul |date=2010-01-07 |title=Easy parallelization with Bash in Linux |url=http://coldattic.info/post/7/ |access-date=2025-01-26 |website=coldattic.info |language=en}}</ref><ref>{{Cite web |title=xargs(1) - Linux manual page |url=https://www.man7.org/linux/man-pages/man1/xargs.1.html |access-date=2025-01-26 |website=www.man7.org}}</ref><ref>{{Cite web |title=Controlling Parallelism (GNU Findutils 4.10.0) |url=https://www.gnu.org/software/findutils/manual/html_node/find_html/Controlling-Parallelism.html |access-date=2025-01-26 |website=www.gnu.org}}</ref> In [[Go (programming language)|Go]], called worker pool: <syntaxhighlight lang="go"> package main import ( "fmt" "time" ) func worker(id int, jobs <-chan int, results chan<- int) { for j := range jobs { fmt.Println("worker", id, "started job", j) time.Sleep(time.Second) fmt.Println("worker", id, "finished job", j) results <- j * 2 } } func main() { const numJobs = 5 jobs := make(chan int, numJobs) results := make(chan int, numJobs) for w := 1; w <= 3; w++ { go worker(w, jobs, results) } for j := 1; j <= numJobs; j++ { jobs <- j } close(jobs) for a := 1; a <= numJobs; a++ { <-results } } </syntaxhighlight> It will print: <syntaxhighlight lang="console"> $ time go run worker-pools.go worker 1 started job 1 worker 2 started job 2 worker 3 started job 3 worker 1 finished job 1 worker 1 started job 4 worker 2 finished job 2 worker 2 started job 5 worker 3 finished job 3 worker 1 finished job 4 worker 2 finished job 5 real 0m2.358s </syntaxhighlight><ref>{{Cite web |title=Go by Example: Worker Pools |url=https://gobyexample.com/worker-pools |access-date=2021-07-27 |website=gobyexample.com}}</ref><ref>{{Cite web |title=Effective Go - The Go Programming Language |url=https://golang.org/doc/effective_go#channels |access-date=2021-07-27 |website=golang.org |quote=another approach that manages resources well is to start a fixed number of handle goroutines all reading from the request channel. The number of goroutines limits the number of simultaneous calls to process}}</ref><ref>{{Cite web |title=The Case For A Go Worker Pool β brandur.org |url=https://brandur.org/go-worker-pool |access-date=2021-07-27 |website=brandur.org |quote=Worker pools are a model in which a fixed number of m workers (implemented in Go with goroutines) work their way through n tasks in a work queue (implemented in Go with a channel). Work stays in a queue until a worker finishes up its current task and pulls a new one off.}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)