Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • Or search :

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)
uses Process;
var
  P: TProcess;
begin
  ...
  P.Execute;
  if not P.WaitOnExit(5000) then P.Terminate(0);
end.
require 'timeout'
Timeout::timeout(5) { p }

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