Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Last active August 29, 2015 14:05
Show Gist options
  • Save sasezaki/6b3de5ce8d285bd52ea6 to your computer and use it in GitHub Desktop.
Save sasezaki/6b3de5ce8d285bd52ea6 to your computer and use it in GitHub Desktop.
$ php -v
PHP 5.5.14-2+deb.sury.org~precise+1 (cli) (built: Jul 2 2014 12:07:02)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
<?php
//
//real 0m2.103s
//user 0m0.160s
//sys 0m0.448s
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/vendor/rdlowrey/after/lib/functions.php';
$client = new Artax\Client;
$arrayOfPromises = $client->requestMulti(iterator_to_array(requests_generator()));
list($errors, $responses) = After\some($arrayOfPromises)->wait();
foreach ($responses as $key => $response) {
//printf(
// "%s | HTTP/%s %d %s\n",
// $key,
//$response->getProtocol(),
//$response->getStatus(),
//$response->getReason()
echo $response->getBody();
//);
}
function requests_generator()
{
// IDの若いGitHubユーザー20人
$loginIds = [
'mojombo', 'defunkt', 'pjhyett', 'wycats', 'ezmobius',
'ivey', 'evanphx', 'vanpelt', 'wayneeseguin', 'brynary',
'kevinclark', 'technoweenie', 'macournoyer', 'takeo', 'Caged',
'topfunky', 'anotherjesse', 'roland', 'lukas', 'fanvsfan',
];
foreach ($loginIds as $loginId) {
yield $loginId => 'https://github.com/'.$loginId.'.keys';
}
}
<?php
// borrowed from http://d.hatena.ne.jp/hnw/20140824
// real 0m3.292s
// user 0m0.160s
// sys 0m0.384s
use GuzzleHttp\Client;
use GuzzleHttp\Event\CompleteEvent;
require_once __DIR__.'/vendor/autoload.php';
$client = new Client();
$client->sendAll(requests_generator($client), [
'parallel' => 4,
'complete' => function (CompleteEvent $event) {
// echo 'Completed request to ' . $event->getRequest()->getUrl() . "\n";
echo 'Response: ' . $event->getResponse()->getBody() . "\n\n";
}
]);
function requests_generator($client)
{
// IDの若いGitHubユーザー20人
$loginIds = [
'mojombo', 'defunkt', 'pjhyett', 'wycats', 'ezmobius',
'ivey', 'evanphx', 'vanpelt', 'wayneeseguin', 'brynary',
'kevinclark', 'technoweenie', 'macournoyer', 'takeo', 'Caged',
'topfunky', 'anotherjesse', 'roland', 'lukas', 'fanvsfan',
];
foreach ($loginIds as $loginId) {
yield $client->createRequest('GET', 'https://github.com/'.$loginId.'.keys');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment