It depends. Checking the context (btw "if err := ctx.Err(); err != nil { return err }" is enough, no need for "select") is cheap but not free. So partition for reasonable sized chunks of work.
Tamás Gurunandan Bhat a következőt írta (2023. július 21., péntek, 9:21:15 UTC+2): > Sorry - I meant ..."calling select once per line...." be a good point > > On Fri, Jul 21, 2023 at 12:41 PM Gurunandan Bhat <gb...@pobox.com> wrote: > >> Hi, >> >> Are there any common patterns that standard and 3rd party library >> functions use to implement cancelling a context passed to it? I have looked >> at the source of exec.CommandContext and it starts the process and spawns a >> goroutine that kills the process when ctx.Done() is closed. That looks >> simple enough. >> >> It seems to me that the key to implementing cancellation enabled function >> is to look at specific points inside the function where ctx.Done() is >> checked for. As an example, if I want to create a cancellable function that >> reads lines from a file, would select'ing on ctx.Done() be a good place? >> >> Thanks. >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/134fe37d-4c8b-4360-934a-6b89c37bc581n%40googlegroups.com.