Skip to content

Commit

Permalink
prompt: Close channel when done (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshineplan authored Mar 19, 2024
1 parent d1b4928 commit a0e3fd9
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,18 @@ func (prompt *Prompt) Execute(ai ai.AI, input []string, prefix string) (<-chan R
return nil, err
}
c := make(chan Result, len(prompts))
go workers.RunSlice(prompt.workers, prompts, func(i int, p string) {
ctx, cancel := context.WithTimeout(context.Background(), prompt.d)
defer cancel()
resp, err := ai.Chat(ctx, p)
if err != nil {
c <- Result{i, p, nil, err}
} else {
c <- Result{i, p, resp.Results(), nil}
}
})
go func() {
workers.RunSlice(prompt.workers, prompts, func(i int, p string) {
ctx, cancel := context.WithTimeout(context.Background(), prompt.d)
defer cancel()
resp, err := ai.Chat(ctx, p)
if err != nil {
c <- Result{i, p, nil, err}
} else {
c <- Result{i, p, resp.Results(), nil}
}
})
close(c)
}()
return c, nil
}

0 comments on commit a0e3fd9

Please sign in to comment.