Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AYehia0 opened this issue Jan 19, 2023 · 1 comment

Comments

@AYehia0
Copy link

AYehia0 commented Jan 19, 2023

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 😃

@cognusion
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants