SoFunction
Updated on 2025-03-04

golang 40 lines of code implement a general coroutine pool

Code Repository

goroutine-pool

golang's coroutine management

The golang coroutine mechanism conveniently solves the problem of concurrent programming, but coroutines are not without overhead, so the number needs to be appropriately limited.

Code that does not use coroutine pools (the sample code is implemented using chan, the code is a bit verbose)

func (p *converter) upload(bytes [][]byte) ([]string, error) {
  ch := make(chan struct{}, 4)
  wg := &{}
  (len(bytes))
  ret := make([]string, len(bytes))
  // Upload  for index, item := range bytes {
    ch <- struct{}{}
    go func(index int, imageData []byte) {
      defer func() {
        ()
        <-ch
      }()
      link, err := (imageData, ("%", ().UnixNano()))
      if err != nil {
        ("Uploading image failed", ())
        return
      }
      ret[index] = link
    }(index, item)
  }
  ()
  return ret, nil
}

There are two requirements to be realized:

Limit the maximum number of coroutines, this example is 4

Wait for all coroutines to complete, this example is the bytes slice length

Code using coroutine pool

func (p *converter) upload(bytes [][]byte) ([]string, error) {
  ret := make([]string, len(bytes))
  pool := goroutine_pool.New(4, len(bytes))

  for index, item := range bytes {
    index := index
    item := item
    (func() {
      link, err := (item, ("%", ().UnixNano()))
      if err != nil {
        ("Uploading image failed", ())
        return
      }

      ret[index] = link
    })
  }
  ()
  return ret, nil
}

You can see that the biggest difference is that you only need to pay attention to the business logic, and concurrency control and waiting have been taken over by the coroutine pool.

Summarize

The above is the golang 40-line code implementation universal coroutine pool introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!