Skip to content

Instantly share code, notes, and snippets.

@JingwenTian
Forked from avalanche123/timeout.php
Created November 24, 2016 04:17
Show Gist options
  • Save JingwenTian/a4985cce6c275c6b8eec8b108784e8bb to your computer and use it in GitHub Desktop.
Save JingwenTian/a4985cce6c275c6b8eec8b108784e8bb to your computer and use it in GitHub Desktop.

Revisions

  1. @avalanche123 avalanche123 created this gist Jul 10, 2012.
    47 changes: 47 additions & 0 deletions timeout.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    <?php

    class TimeoutException extends RuntimeException {}

    class Timeout
    {
    private $active;

    public function set($seconds)
    {
    $this->active = true;

    declare(ticks = 1);
    pcntl_signal(SIGALRM, array($this, 'handle'), true);
    pcntl_alarm($seconds);
    }

    public function clear()
    {
    $this->active = false;
    }

    public function handle($signal)
    {
    echo "received signal\n";
    if ($this->active) {
    throw new TimeoutException();
    }
    }
    }

    $timeout = new Timeout();
    $start = microtime(true);
    try {
    echo "setting timeout to 1 second\n";
    $timeout->set(1); // set a 1 second timeout
    echo "sleeping for 10 seconds\n";
    sleep(10); // some long running operation...
    echo "clearing 1 second timeout\n";
    $timeout->clear(); // clear timeout
    } catch(TimeoutException $e) {
    // timed out
    echo "caught a TimeoutException\n";
    }

    $total = microtime(true) - $start;
    echo "time spent {$total}\n";