Logo

Programming-Idioms

  • Python
  • C++
  • Go

Idiom #230 Timeout

Cancel an ongoing processing p if it has not finished after 5s.

import "context"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
p(ctx)

p is responsible for shutting down gracefully when ctx is canceled.
def main(p) do
  p
  |> Task.async()
  |> Task.await()
end

New implementation...
< >
programming-idioms.org