Skip to content

Instantly share code, notes, and snippets.

@diyism
Last active March 30, 2021 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diyism/b66dd446759b844de9a43fa6f3f75ad6 to your computer and use it in GitHub Desktop.
Save diyism/b66dd446759b844de9a43fa6f3f75ad6 to your computer and use it in GitHub Desktop.
coroutine vs callback (php7.3+swoole4.3.0)
<?php
/*===========compile php7.3+swoole4.3.0===============
$sudo apt-get install re2c bison autoconf zlib1g-dev libssl-dev libcurl4-openssl-dev libargon2-0-dev libsodium-dev
$git clone -b PHP-7.3 --depth 1 https://github.com/php/php-src.git
$cd php-src/
$mkdir ext/swoole && curl -s "https://codeload.github.com/swoole/swoole-src/tar.gz/v4.3.0" | tar xvz --strip 1 -C ext/swoole
$./buildconf --force
$./configure --prefix=/usr/local/php7 --disable-all --enable-cli --disable-cgi --disable-fpm --disable-phpdbg --enable-bcmath --enable-hash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --enable-sockets --enable-pdo --with-sodium --with-password-argon2 --with-sqlite3 --with-pdo-sqlite --with-pcre-regex --with-zlib --with-openssl-dir --enable-openssl --enable-swoole
$time make -j `cat /proc/cpuinfo | grep processor | wc -l`
$sudo make install
$sudo rm -f /usr/bin/php
$sudo ln -s /usr/local/php7/bin/php /usr/bin/php
*/
$serv=new \swoole_http_server("127.0.0.1", 9503, SWOOLE_BASE);
$serv->on('request', function($req, $resp)
{
$clis=[];
$clis[1]=new Swoole\Coroutine\Http\Client('1.1.1.1', 443, 1);
$clis[1]->set(['timeout' => 10]);
$headers=
[
'Host'=>"1.1.1.1:443",
"User-Agent"=>'Chrome/49.0.2587.3',
'Accept'=>'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding'=>'gzip',
];
$clis[1]->setHeaders($headers);
go(function() use (&$clis, &$resp)
{
$ret=$clis[1]->get('/');
callback($clis, 1, $resp);
});
$clis[2]=new Swoole\Coroutine\Http\Client('1.1.1.1', 443, 1);
$clis[2]->set(['timeout'=>10]);
$headers=
[
'Host'=>"1.1.1.1:443",
"User-Agent"=>'Chrome/49.0.2587.3',
'Accept'=>'text/html,application/xhtml+xml,application/xml',
'Accept-Encoding'=>'gzip',
];
$clis[2]->setHeaders($headers);
go(function() use (&$clis, &$resp)
{
$ret=$clis[2]->get('/');
callback($clis, 2, $resp);
});
});
$serv->start();
function callback($clis, $cli_id, &$resp)
{
$body=htmlentities(substr($clis[$cli_id]->body, 0, 100));
if (!$body)
{
return;
}
foreach ($clis as $k=>&$c)
{
if ($cli_id!==$k)
{
$c->close();
}
}
$clis=null;
$resp->end($cli_id.':'.$body);
}
//=============================3 requests only cost 1 request's time======================================
echo date('Y-m-d H:i:s')."\n";
$rslt=[];
\Co\run(function()use(&$rslt){$wg=new \Co\WaitGroup();$wg->add(3); //swoole parallelization 1st step
for ($i=0; $i<3; ++$i)
{
go(function()use($wg, &$rslt){ //swoole parallelization 2nd step
$rslt[]=substr(file_get_contents('https://guagle.com'), 0, 20);
$wg->done();}); //swoole parallelization 3rd step
}
$wg->wait();}); //swoole parallelization 4th step
var_export($rslt);echo "\n";
echo date('Y-m-d H:i:s').'<br>';
//=================3 requests only cost 1 request's time==============in symfony swoole bundle========================
//remove one line of "Co\run(function(){...." and add one line of "\Swoole\Runtime::enableCoroutine();"
\Swoole\Runtime::enableCoroutine(SWOOLE_HOOK_NATIVE_CURL);
$wg=new \Co\WaitGroup();$wg->add(3); //swoole parallelization 1st step
for ($i=0; $i<3; ++$i)
{
go(function()use($wg, &$rslt){ //swoole parallelization 2nd step
$rslt[]=substr(file_get_contents('https://guagle.com'), 0, 20);
$wg->done();}); //swoole parallelization 3rd step
}
$wg->wait(); //swoole parallelization 4th step
使用 curl的话, 每个请求需重新创建$ch=curl_init();, 不能放到对象元素$this->_ch里共用,
结束的时候需要关闭和销毁$ch: curl_close($ch);unset($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment