Created
August 2, 2013 20:07
-
-
Save m6w6/6143009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$async = new AsyncHttp; | |
for ($i = 1; $i < $argc; ++$i) { | |
$async->attach(new HttpRequest($argv[$i]), function ($req) use (&$state) { | |
$state = ""; | |
printf("\n%d < %s\n", $req->getResponseCode(), $req->getUrl()); | |
}); | |
} | |
while ($async()) { | |
printf("\r... doing some other work%s", $state .= "."); | |
} | |
class AsyncHttp extends HttpRequestPool { | |
protected $callbacks; | |
function __construct() { | |
parent::__construct(); | |
$this->callbacks = new SplObjectStorage; | |
} | |
function attach(HttpRequest $req, callable $cb) { | |
parent::attach($req); | |
$this->callbacks[$req] = $cb; | |
} | |
function __invoke() { | |
try { | |
$this->socketSelect(1); | |
$this->socketPerform(); | |
} catch (Exception $e) { | |
echo $e; | |
return false; | |
} | |
foreach ($this->getFinishedRequests() as $req) { | |
$this->detach($req); | |
call_user_func($this->callbacks[$req], $req); | |
} | |
return count($this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment