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.
Iterate over process
<?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);
}
}
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"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment