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

when I was using this codes, some panics happened! #38

Open
FunkyGod opened this issue Jan 9, 2022 · 1 comment
Open

when I was using this codes, some panics happened! #38

FunkyGod opened this issue Jan 9, 2022 · 1 comment

Comments

@FunkyGod
Copy link

FunkyGod commented Jan 9, 2022

`package goTest

import (
"fmt"
"github.com/Jeffail/tunny"
"strconv"
"testing"
"time"
)

func Test_pools(t *testing.T) {

pool := tunny.NewFunc(11, func(payload interface{}) interface{} {
	fmt.Println(payload.(int))
	test1(payload.(int))
	time.Sleep(time.Second)
	return nil
})
defer pool.Close()
for i := 0; i < 10; i++ {
	go pool.Process(i)
}
pool.Close()

}
func test1(i int){
fmt.Println("a"+strconv.Itoa(i))
}
`

problem
`GOROOT=/usr/local/Cellar/go/1.14.6/libexec #gosetup
GOPATH=/Users/gudaixin/go #gosetup
/usr/local/Cellar/go/1.14.6/libexec/bin/go test -c -o /private/var/folders/ty/2yjqwcmn1xv6d9v7blg39x0h0000gn/T/___Test_pools_in_ProjectGoModule_src_goTest ProjectGoModule/src/goTest #gosetup
/usr/local/Cellar/go/1.14.6/libexec/bin/go tool test2json -t /private/var/folders/ty/2yjqwcmn1xv6d9v7blg39x0h0000gn/T/___Test_pools_in_ProjectGoModule_src_goTest -test.v -test.run ^Test_pools$ #gosetup
=== RUN Test_pools
6
a6
7
a7
8
a8
9
a9
1
a1
panic: the pool is not running

goroutine 34 [running]:
github.com/Jeffail/tunny.(*Pool).Process(0xc0000a4640, 0x1114780, 0xc0000a61c0, 0xa9978700000000f, 0x61da5e0f)
/Users/gudaixin/go/pkg/mod/github.com/!jeffail/tunny@v0.1.4/tunny.go:158 +0x13d
created by ProjectGoModule/src/goTest.Test_pools
/Users/gudaixin/Public/PrivatePersonGoProject/ProjectGoModule/src/goTest/pool_test.go:21 +0xb1

Process finished with exit code 1
`

@mihaitodor
Copy link
Contributor

@FunkyGod Thanks for trying out Tunny! It looks like you're missing some code logic to wait for the goroutines to complete before Test_pools exits and calls pool.Close(). It's up to the user to ensure that the pool doesn't close before all these goroutines complete. Here's one way to do that (although you'll need to make sure that all the goroutines do complete after a finite amount of time):

package tunny_test

import (
	"fmt"
	"strconv"
	"sync"
	"testing"
	"time"

	"github.com/Jeffail/tunny"
)

func Test_pools(t *testing.T) {
	wg := sync.WaitGroup{}
	pool := tunny.NewFunc(11, func(payload interface{}) interface{} {
		defer wg.Done()
		fmt.Println(payload.(int))
		test1(payload.(int))
		time.Sleep(time.Second)
		return nil
	})
	defer pool.Close()

	wg.Add(10)
	for i := 0; i < 10; i++ {
		go pool.Process(i)
	}

	wg.Wait()
}

func test1(i int) {
	fmt.Println("a" + strconv.Itoa(i))
}

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