Coder Social home page Coder Social logo

Comments (7)

GeertJohan avatar GeertJohan commented on September 28, 2024

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.

GeertJohan avatar GeertJohan commented on September 28, 2024

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.

GeertJohan avatar GeertJohan commented on September 28, 2024

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.

VarunBatraIT avatar VarunBatraIT commented on September 28, 2024

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.

GeertJohan avatar GeertJohan commented on September 28, 2024

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.

rakyll avatar rakyll commented on September 28, 2024

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.

VarunBatraIT avatar VarunBatraIT commented on September 28, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.