-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vertexai(test): Run corpora test in go coroutine to reduce test runtime #10841
Conversation
worker := func() { | ||
for corpora := range corporaChan { | ||
if ucr.shouldSkip(corpora.Name) { | ||
fmt.Printf("Skipping file: %s\n", corpora.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmt
is not usually concurrency-safe (nothing bad will happen but output can get mixed up between goroutines).
Use log.Printf
inside goroutines instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
} | ||
doneChan <- true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this code will work fine, but since your workers don't actually return any results, I recommend a slightly different pattern, using a WorkGroup
.
This example shows how: https://gobyexample.com/waitgroups
Then you won't need the loop clearing the doneChan
in the main goroutine; you'll just do a wait on the group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
if err != nil { | ||
log.Fatalf("Failed to decode bytes: %v", err) | ||
} | ||
workerCountChan := make(chan int, 10) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand what this channel is for -- can you please explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use this channel to manage up to 10 corpora run simultaneously, preventing them from all launching at once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, thanks for clarifying.
In this case the naming or a comment should be used to make this clearer. Also, for channels like this it's customary to have them carry no information, e.g. chan struct{}
-- this further clarifies that the contents of the channel have no meaning.
The channel can be named something like workLimiter
or concurrencyLimiter
, or if you prefer to keep your channel name, just add a comment explaining what it's for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks for helping me improve it!
No description provided.