Git Product home page Git Product logo

Comments (6)

niemeyer avatar niemeyer commented on August 21, 2024

Yes, it is intentional. If nothing ever runs there's no way to properly flag the tomb's death. You should also be able to use the Dead and Dying channels instead of Wait for holding out, for instance, and there's no means for us to guess that these channels have to be closed if the tomb is never called.

from tomb.

niemeyer avatar niemeyer commented on August 21, 2024

As a side note, the example provided feels unnecessarily hackish. You can simply do the obvious:

func rmain() error {
        if somethingBad {
                return err
        }
        ...
        obj.tomb.Go(obj.action)
        return obj.tomb.Wait()
}

I also more often organize it as:

func StartFoo() (*Foo, error) {
        ...
        foo.tomb.Go(foo.loop)
        return foo, nil
}

func (foo *Foo) Stop() error {
        foo.tomb.Kill(nil)
        return foo.tomb.Wait()
}

from tomb.

aktau avatar aktau commented on August 21, 2024

Ok, thanks!

The reason I wanted to do it is because I have code between the Go call and the Wait call, like this:

        obj.tomb.Go(obj.action)

        if err := ...; err != nil {
            // return err // whoops, no waiting, can't do this
            obj.tomb.Kill(err) // at least we'll wait, but it's also going to execute functions it shouldn't
        }

        if err := ...; err != nil {
            // ...
        }

        return obj.tomb.Wait()

from tomb.

niemeyer avatar niemeyer commented on August 21, 2024

There are two easy ways to solve the problem:

  1. Do not call Go while you're not ready to spawn a new goroutine
  2. Move the logic that can fail to within the goroutine

Both sound like good advices despite the specific issue being discussed.

from tomb.

aktau avatar aktau commented on August 21, 2024

Do not call Go while you're not ready to spawn a new goroutine

It's not that I'm not ready, in fact it's better if the goroutine already started before I run my other functions.

Move the logic that can fail to within the goroutine

Unfortunately, they need to run concurrently to the goroutine. Or are you saying I should run an unmonitored goroutine within the Go-routine?

from tomb.

niemeyer avatar niemeyer commented on August 21, 2024

You can run as many things concurrently as desired by just firing more monitored goroutines from any goroutine already being monitored. For example:

func StartFoo() (*Foo, error) {
        ...
        foo.tomb.Go(foo.loop)
        return foo, nil
}

func (foo *Foo) Stop() error {
        foo.tomb.Kill(nil)
        return foo.tomb.Wait()
}

func (foo *Foo) loop() error {
        foo.tomb.Go(foo.task1)
        foo.tomb.Go(foo.task2)
        ...
}

from tomb.

Related Issues (17)

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.