Comments (7)
I'm wondering how you would be able to cancel a running goroutine.... I really can't think of anything without having the goroutine 'knowing' how to stop (select receiving a channel periodically and acting when it's closed)
from coop.
I don't think it's very good that a 918 star project makes an impossible promise in README.md. Please change the documentation for coop.Timeout (or remove it all together since everyone using it is expecting behavior thats not actually happening.)
from coop.
The only case where behaviour will be as expected is when main() ends before the fn is finished.
func main() {
done := coop.Timeout(time.Second, func() {
time.Sleep(2 * time.Second)
fmt.Println("Hello world")
})
<-done // will return false, because timeout occurred
}
When adding a sleep after the receive on the channel done
, it still lets the fn finish (it isn't canceled)
func main() {
done := coop.Timeout(time.Second, func() {
time.Sleep(2 * time.Second)
fmt.Println("Hello world")
})
<-done // will return false, because timeout occurred
time.Sleep(3 * time.Second)
}
And after that coop panics:
Hello world
panic: send on closed channel
goroutine 6 [running]:
github.com/rakyll/coop.func·005()
/home/geertjohan/src/github.com/rakyll/coop/coop.go:79 +0x66
created by github.com/rakyll/coop.Timeout
/home/geertjohan/src/github.com/rakyll/coop/coop.go:80 +0x1b8
goroutine 1 [sleep]:
time.Sleep(0xb2d05e00)
/home/geertjohan/go/src/runtime/time.go:59 +0x105
main.main()
/home/geertjohan/blah.go:16 +0x64
exit status 2
from coop.
I am curious that "ToDo" is possible?
I need to know if once timeout can I kill that goroutine? or simply put <-done because I don't need it anymore?
from coop.
It won't kill the goroutine. The Timeout function doesn't actually implement what it promises. It's imposible to do it like that.
You're better off writing your own concurrency patterns.
from coop.
The Go runtime doesn't provide mechanisms to stop an executing goroutine. You may structure your goroutine to react to a signal and return to exit. Such a scenario will require you to develop a mechanism momentarily stop executing your actual task and try to read the signal (most likely from a channel).
Thanks for reminding me that I should have removed the Timeout function.
from coop.
In my use case this isn't a serious limitation. I can listen to the signal. I am not sure how other developers will react to this. Thanks to both for your comments.
from coop.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coop.