pb icon indicating copy to clipboard operation
pb copied to clipboard

[QUESTION] How to get multiple/single progressbars for concurrent processes ?

Open AYehia0 opened this issue 3 years ago • 1 comments

I am trying to create one bar or multiple ones for the length of the download list, I am using goroutines to download the list really fast. as follows : Inside the main

if soundData.Kind == "playlist" {
	var wg sync.WaitGroup
	plDownloadTracks := getPlaylistDownloadTracks(soundData, clientId)

	for _, dlT := range plDownloadTracks {

		wg.Add(1)

		go func(dlT []soundcloud.DownloadTrack) {
			defer wg.Done()
			t := getTrack(dlT, true)
			fp := soundcloud.Download(t, downloadPath)

			// silent indication of already existing files
			if fp == "" {
				return
			}
			soundcloud.AddMetadata(t, fp)

		}(dlT)
	}
	wg.Wait()

	fmt.Printf("\n%s Playlist saved to : %s\n", theme.Green("[-]"), theme.Magenta(downloadPath))
	return
}

and here is my Download function :


func Download(track DownloadTrack, dlpath string) string {
	trackName := track.SoundData.Title + "[" + track.Quality + "]." + track.Ext
	path := validateDownload(dlpath, trackName)

	resp, err := http.Get(track.Url)

	if err != nil {
		return ""
	}
	defer resp.Body.Close()

	// check if the file exists
	f, _ := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
	defer f.Close()

        // here I am using the progress bar from "github.com/schollz/progressbar/v3" but it doesn't support pools
	bar := bar.DefaultBytes(
		resp.ContentLength,
		"Downloading",
	)

	io.Copy(io.MultiWriter(f, bar), resp.Body)

	return path
}

Sorry for such a question but I searched the docs and I found nothing, I would really appreciate your help :smiley:

AYehia0 avatar Jan 19 '23 13:01 AYehia0

There is an example for multiple bars at https://github.com/cheggaaa/pb/blob/master/example_multiple_test.go .. To use with v3, change the import line, and remove the ".Prefix()" suffixes to the end of the New calls. If you're properly starting and stopping the bars, it works from inside other goros. You can dynamically add new bars to the pool with .Add(pb). I haven't tested what happens with large numbers (multiple screens) of bars, but have used it with less than a screenful with great success.

cognusion avatar Jan 31 '23 19:01 cognusion