Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Elixir

Idiom #230 Timeout

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

def main(p) do
  p
  |> Task.async()
  |> Task.await()
end
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.

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