Skip to content

Instantly share code, notes, and snippets.

@proclnas
Last active August 21, 2017 17:56
Show Gist options
  • Save proclnas/91bb6989f3c4da44d5ca28f2412da4d1 to your computer and use it in GitHub Desktop.
Save proclnas/91bb6989f3c4da44d5ca28f2412da4d1 to your computer and use it in GitHub Desktop.
<?php
// run with php7
// php ex-pthreads.php -t thread
$opt = getopt('t:');
if (!isset($opt['t']))
exit (sprintf('php %s -t threads' . PHP_EOL, $argv[0]));
$myArray = [
'http://wikipedia.org',
'http://yahoo.com',
'http://reddit.com',
'http://google.co.in',
'http://runrun.it',
'http://www.geradordecnpj.org',
'http://www.oceanbrasil.com',
'http://php.net',
];
$pool = new Pool($opt['t']);
foreach ($myArray as $site) {
$pool->submit(new class($site) extends Threaded {
public function __construct($site) {
$this->site = $site;
}
public function run() {
echo sprintf('Getting [%s]'.PHP_EOL, $this->site);
file_get_contents($this->site);
echo sprintf('Finished [%s]'.PHP_EOL, $this->site);
}
private $site;
});
}
while ($pool->collect()) continue;
$pool->shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment