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

Allow for initialising a pool without starting it #124

Merged
merged 1 commit into from Feb 18, 2018

Conversation

wjessop
Copy link
Contributor

@wjessop wjessop commented Feb 16, 2018

Maybe fixes #123

It's not perfect however: https://youtu.be/Oo_L_MHvEmo. In the video pb.go is:

package main

import (
	"math/rand"
	"os"
	"sync"
	"time"

	"github.com/wjessop/pb"
)

func main() {
	// create bars
	first := pb.New64(1000).Prefix("one ").SetUnits(pb.U_BYTES)
	second := pb.New64(1000).Prefix("two ").SetUnits(pb.U_BYTES)

	first.Output = os.Stderr
	second.Output = os.Stderr

	// start pool
	// pool, err := pb.StartPool(first, second)
	pool := pb.NewPool()
	pool.Output = os.Stderr
	err := pool.Start()
	if err != nil {
		panic(err)
	}

	// update bars
	wg := new(sync.WaitGroup)
	for _, bar := range []*pb.ProgressBar{first, second} {
		wg.Add(1)
		go func(cb *pb.ProgressBar) {
			for n := 0; n < 200; n++ {
				// cb.Increment()
				time.Sleep(time.Millisecond * time.Duration(rand.Intn(100)))
				cb.Add(5)
			}
			cb.Finish()
			wg.Done()
		}(bar)
	}
	wg.Wait()
	// close pool
	pool.Stop()
}

@cheggaaa cheggaaa merged commit 01ee04e into cheggaaa:master Feb 18, 2018
@cheggaaa
Copy link
Owner

Thank you! Merged

first.Output = os.Stderr
second.Output = os.Stderr

But these lines in example don't make sense. Only pool.Output has an effect in multiple bars case

@wjessop
Copy link
Contributor Author

wjessop commented Feb 18, 2018

Any idea why the second run (see video) prints out three lines instead of two? It happens to me when running the example about 50% of the time.

@cheggaaa
Copy link
Owner

@wjessop how are you stopping the program?
pool waits to Interrupt signal for correct stop (or call pool.Stop() )

@wjessop
Copy link
Contributor Author

wjessop commented Feb 18, 2018

I'm running the program with:

go run pb.go 

Go version go version go1.10 darwin/amd64

I'm just letting the program exit normally, so pool.Stop() should be getting called on the second to last line of the code above.

@cheggaaa
Copy link
Owner

Oh, I'm running your code whole and see the same result.
It's the small mistake in your code, you forgot to add bars objects to pb.NewPool
Just replace to pb.NewPool(first, second).

And I think I found new bug... in go 1.10 pool does not sense stop with ctrl+c (in go 1.9 all ok)

@wjessop
Copy link
Contributor Author

wjessop commented Feb 19, 2018

When I add the progress bars to the pool the output it works as expected:

pool := pb.NewPool()
pool.Output = os.Stderr
pool.Add(first)
pool.Add(second)

I also noticed that if I ctrl-c the process nothing happens. If I ctrl-c the process twice it does interrupt.

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

Successfully merging this pull request may close these issues.

Output is broken when setting output to Stderr
2 participants