Comments (7)
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.
I think it would be better for Wait()
to just return.
from tomb.
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.
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.
Oh, yes, it's simpler this way!
from tomb.
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.
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)
- please make a release HOT 4
- Wait() never unblocks when no goroutine is running HOT 1
- Panics can happen if the first callback finishes very quickly HOT 2
- Would be nice to have `Running()` and `Terminating()`
- Please keep source gofmt'ed HOT 1
- Bug: Tomb `Wait` blocks forever when no Goroutine is ever spawned. HOT 2
- Inactivity ... I would like to volunteer/help-out
- Seemingly impossible panic: tomb.Go called after all goroutines terminated HOT 2
- go test error
- License BSD3
- tomb kills self after the last goroutine completes HOT 1
- Waiting when no tomb.Go call has been made HOT 6
- Usage Question / panic, tomb.go called after all goroutines terminated
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 tomb.