Git Product home page Git Product logo

Comments (7)

niemeyer avatar niemeyer commented on August 21, 2024 1

I assume your concern is that "something" might be an empty list.

You can also handle this situation by doing the iteration inside the first goroutine:

t.Go(func() error {
        for _, s := range something {
                t.Go(func() error { ... })
        }
})

It's always safe to fire new goroutines inside any prior goroutine.

from tomb.

vincentbernat avatar vincentbernat commented on August 21, 2024

I think it would be better for Wait() to just return.

from tomb.

niemeyer avatar niemeyer commented on August 21, 2024

That's by design. The whole point of a tomb is to wait for work to finish, and if we never start the work it will never finish. If we didn't block on the work starting, concurrent calls to Wait might immediately unblock, and potentially lead to code that worked in tests but not in production given different execution ordering.

If you're finding issues with this, the easy rule of thumb is this: don't hand off a tomb if you don't plan on starting the work. If an error happened, return the error and not the tomb. After you give access to the tomb, do start the work eventually since there's logic waiting for it.

That was the design choice and the rationale. We cannot change it now without breaking people's code, so this isn't up for discussion anymore unfortunately.

from tomb.

vincentbernat avatar vincentbernat commented on August 21, 2024

Understood.

In my code, I therefore do this:

initialized := make(chan struct{})
t.Go(func() error {
    <- initialized
    return nil
})
for _, s := range something {
    t.Go(func() { ... })
}
close(initialized)

This way, I get a dummy goroutine, alive the time to spawn the other goroutines.

from tomb.

vincentbernat avatar vincentbernat commented on August 21, 2024

Oh, yes, it's simpler this way!

from tomb.

freeekanayaka avatar freeekanayaka commented on August 21, 2024

Yeah my use case was similar to @vincentbernat's, i.e.:

t := tomb.Tomb{}
doStuffWhich_MIGHT_EVENTUALLY_InvolveSpawningGoroutines(t)
t.Kill(nil) // I'm fed up and I want to quit gracefully
t.Wait()

I understand this conflicts with the ability of spawning "producers" (t.Go) and "consumers" (t.Wait) in parallel tho.

Not a big deal, as @niemeyer's workaround works too.

from tomb.

yanggeorge avatar yanggeorge commented on August 21, 2024

I assume your concern is that "something" might be an empty list.

You can also handle this situation by doing the iteration inside the first goroutine:

t.Go(func() error {
        for _, s := range something {
                t.Go(func() error { ... })
        }
})

It's always safe to fire new goroutines inside any prior goroutine.

nice๏ผ

from tomb.

Related Issues (14)

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.