Skip to content

Instantly share code, notes, and snippets.

@fezfez
Last active August 29, 2015 14:23
Show Gist options
  • Save fezfez/bcc077d4f134c55c2786 to your computer and use it in GitHub Desktop.
Save fezfez/bcc077d4f134c55c2786 to your computer and use it in GitHub Desktop.

Revisions

  1. fezfez revised this gist Jun 24, 2015. 2 changed files with 0 additions and 11 deletions.
    10 changes: 0 additions & 10 deletions processIterator.php
    Original file line number Diff line number Diff line change
    @@ -42,16 +42,6 @@ public function valid()

    $processIterator = new ProcessIterator(__DIR__ . '/tmpeee.sh');

    foreach ($processIterator as $i => $result) {
    echo "$i Time : " .(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ", result : " . $result . "\n";
    if ($i == 9) {
    echo "php sleeping for 8\n";
    sleep(8);
    }
    }

    $processIterator = new ProcessIterator(__DIR__ . '/slowProcess.sh');

    foreach ($processIterator as $i => $result) {
    echo "$i Time : " .(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ", result : " . $result . "\n";
    if ($i == 9) {
    1 change: 0 additions & 1 deletion slowProcess.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    echo '{"value":"first valuddddde"} sleep 2'

    sleep 2
  2. fezfez created this gist Jun 24, 2015.
    61 changes: 61 additions & 0 deletions processIterator.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?php


    class ProcessIterator implements Iterator
    {
    private $stream;
    private $command;
    private $iteration = 0;

    public function __construct($command)
    {
    $this->command = $command;
    }

    public function rewind()
    {
    $this->stream = popen($this->command, 'r');
    $this->iteration = 0;
    }

    public function current()
    {
    return fgets($this->stream);
    }

    public function key()
    {
    return $this->iteration;
    }

    public function next()
    {
    $this->iteration++;
    }

    public function valid()
    {
    return !feof($this->stream);
    }
    }


    $processIterator = new ProcessIterator(__DIR__ . '/tmpeee.sh');

    foreach ($processIterator as $i => $result) {
    echo "$i Time : " .(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ", result : " . $result . "\n";
    if ($i == 9) {
    echo "php sleeping for 8\n";
    sleep(8);
    }
    }

    $processIterator = new ProcessIterator(__DIR__ . '/slowProcess.sh');

    foreach ($processIterator as $i => $result) {
    echo "$i Time : " .(microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) . ", result : " . $result . "\n";
    if ($i == 9) {
    echo "php sleeping for 8\n";
    sleep(8);
    }
    }
    16 changes: 16 additions & 0 deletions slowProcess.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@

    echo '{"value":"first valuddddde"} sleep 2'

    sleep 2

    echo '{"value":"first value"}'

    echo '{"value":"other value"}'

    echo '{"value":"new other value"} sleep 5'

    sleep 5

    echo '{"value":"not value"}'

    echo '{"value":"end value"}'