goroutines

Resource Notes
Goroutines & Closures Important caveat on shadowing variable in loops included
Using uiprogress Notes I wrote about trying threadsafe progress bar package

Running CLI tools via goroutines can speed up slow actions like code generation. I prefer to run these types of actions with a buffered channel to throttle the requests and avoid overloading my laptop. 🔥

Here’s an example using Pterm output for reporting progress (no progress bar)1.

Playground - Go :fontawesome-solid-link:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
 "sync"

 "github.com/bitfield/script"
 "github.com/pterm/pterm"
)

func main() {
 pterm.DisableColor()
 concurrentLimit := 4
 type runMe struct {
  title   string
  command string
 }
 runCommands := []runMe{
  {title: "commandtitle", command: "echo 'foo'"},
 }
 var wg sync.WaitGroup
 buffChan := make(chan struct{}, concurrentLimit)
 wg.Add(len(runCommands))
 pterm.Info.Printfln("running cli [%d]", len(runCommands))
 for _, r := range runCommands {
  r := r
  go func(r runMe) {
   buffChan <- struct{}{}
   defer wg.Done()
   if _, err := script.Exec(r.command).Stdout(); err != nil {
    pterm.Error.Printfln("[%s] unable to run: %s, err: %s", r.title, r.command, err)
   } else {
    pterm.Success.Printfln("[%s]", r.title)
   }
   <-buffChan
  }(r)
 }
 wg.Wait()
}

  1. Since things are running concurrently, a single bar isn’t quite accurate. There are libraries that report correctly with goroutines, but as of 2023-03, pterm isn’t one of them. However, it’s under development. ↩︎


Webmentions

(No webmentions yet.)