Skip to content

Instantly share code, notes, and snippets.

@Scriptor
Forked from anonymous/async.php
Created February 1, 2013 17:55
Show Gist options
  • Save Scriptor/4692910 to your computer and use it in GitHub Desktop.
Save Scriptor/4692910 to your computer and use it in GitHub Desktop.
<?php
declare(ticks=10);
// A function called on each tick event
class Counter {
static $a = 0;
static $b = 0;
}
function tick_handler()
{
Counter::$b++;
}
$stream1 = fopen("http://amazon.com", "r");
$stream2 = fopen("http://news.ycombinator.com", "r");
stream_set_blocking($stream1, 0);
stream_set_blocking($stream2, 0);
register_tick_function('tick_handler', 1);
$read = array($stream1, $stream2);
$write = NULL;
$except = NULL;
while (True){
$line = fgets($stream2);
if ($line == "") {
Counter::$a++;
} else {
break;
}
}
echo Counter::$a."\n".Counter::$b."\n";
fclose($stream1);
fclose($stream2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment