CakePHPのコンポーネントで時間のかかる処理をマルチプロセスにしたいのですが、一番最初のfor文のループが2回目になると下記エラーとなってしまいます。
マルチプロセスの処理を入れないと正常に動作します。
また、my.cnfのクエリの最大サイズやタイムアウトなどを変更してみましたがダメでした。
あまりサーバーサイドは得意ではないのでアドバイスお願いします。
《エラー》
error
1Warning Error: Error while sending QUERY packet. PID=27017 in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] 2 3Error: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away 4#0 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(468): PDOStatement->execute(Array) 5#1 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(434): DboSource->_execute('SELECT `Crawlin...', Array) 6#2 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(671): DboSource->execute('SELECT `Crawlin...', Array, Array) 7#3 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(1120): DboSource->fetchAll('SELECT `Crawlin...', false) 8#4 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Model.php(3038): DboSource->read(Object(CrawlingContent), Array) 9#5 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Model.php(3010): Model->_readDataSource('all', Array) 10#6 /home/scraper/public_html/ec_scraper/Controller/Component/ScrapingComponent.php(1414): Model->find('all', Array) 11#7 /home/scraper/public_html/ec_scraper/Controller/Component/ScrapingComponent.php(36): ScrapingComponent->crawler_index_multi() 12#8 /home/scraper/public_html/ec_scraper/Console/Command/ScraperShell.php(65): ScrapingComponent->test() 13#9 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/Shell.php(458): ScraperShell->test() 14#10 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(212): Shell->runCommand('test', Array) 15#11 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(66): ShellDispatcher->dispatch() 16#12 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/cake.php(54): ShellDispatcher::run(Array) 17#13 {main} 18
エラー部分のコード
php
1 //データを分割取得 2 for ($page=1; $page <= $totalPage; $page++) { 3 4 $result = $CrawlingContent->find('all', array( 5 'conditions' => $conditions, 6 'recursive' => -1, 7 'limit' => $limit, 8 'page' => $page, 9 )); 10 11 if ($limit > ($count - $totalAccess)) { 12 $limit = $count - $totalAccess; 13 } 14 15 //初期化 16 $mkp = $pcount; 17 //取得データ 18 for ($access=0; $access < $limit;) { 19 20 if ($mkp > ($limit - $access)) { 21 $mkp = $limit - $access; 22 } 23 24 for ($i=0; $i < $mkp; $i++) { 25 $pid = pcntl_fork(); 26 if( $pid === -1 ) { 27 debug('マルチプロセスのforkに失敗'); 28 exit; 29 }elseif($pid === 0){ 30 //子プロセス 31 debug($result[$i]['CrawlingContent']['url']); 32 exit; 33 } 34 // 親プロセスの処理 35 $processes[] = $pid; 36 } 37 38 foreach($processes as $process) { 39 pcntl_waitpid($process, $status); 40 } 41 42 $access = $access + $i; 43 $totalAccess = $totalAccess + $i; 44 } 45}
子プロセスでのDB接続を試してみましたがダメでした。
php
1 for ($i=0; $i < $pcount; $i++) { 2 $pid = pcntl_fork(); 3 if( $pid === -1 ) { 4 debug('マルチプロセスのforkに失敗'); 5 exit; 6 }elseif($pid === 0){ 7 //子プロセス 8 if ($i===($pcount-1)) { 9 $endPage = ($endPage - $processPage) + $lastProcessPage; 10 } 11 12 $page = $startPage; 13 14 for ($page; $page <= $endPage; $page++) { 15 16 $result = $CrawlingContent->find('all', array( 17 'conditions' => $conditions, 18 'recursive' => -1, 19 'limit' => $limit, 20 'page' => $page, 21 )); 22 foreach ($result as $resultValue) { 23 debug($resultValue['CrawlingContent']['url']); 24 } 25 26 } 27 exit; 28 } 29 // 親プロセスの処理 30 $processes[] = $pid; 31 $startPage = $startPage + $processPage; 32 $endPage = $endPage + $processPage; 33 } 34 35 foreach($processes as $process) { 36 pcntl_waitpid($process, $status); 37 }
エラーコード
Warning Error: Packets out of order. Expected 1 received 3. Packet size=262144 in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: Packets out of order. Expected 4 received 97. Packet size=7496534 in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: Packets out of order. Expected 1 received 99. Packet size=6621798 in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: PDOStatement::execute(): MySQL server has gone away in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: PDOStatement::execute(): MySQL server has gone away in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: PDOStatement::execute(): MySQL server has gone away in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: PDOStatement::execute(): Error reading result set's header in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Warning Error: PDOStatement::execute(): Error reading result set's header in [/home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php, line 468] Error: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away #0 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(468): PDOStatement->execute(Array) #1 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(434): DboSource->_execute('SELECT `Crawlin...', Array) #2 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(671): DboSource->execute('SELECT `Crawlin...', Array, Array) #3 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Datasource/DboSource.php(1120): DboSource->fetchAll('SELECT `Crawlin...', false) #4 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Model.php(3038): DboSource->read(Object(CrawlingContent), Array) #5 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Model/Model.php(3010): Model->_readDataSource('all', Array) #6 /home/scraper/public_html/ec_scraper/Controller/Component/ScrapingComponent.php(1434): Model->find('all', Array) #7 /home/scraper/public_html/ec_scraper/Controller/Component/ScrapingComponent.php(36): ScrapingComponent->crawler_index_multi() #8 /home/scraper/public_html/ec_scraper/Console/Command/ScraperShell.php(65): ScrapingComponent->test() #9 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/Shell.php(458): ScraperShell->test() #10 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(212): Shell->runCommand('test', Array) #11 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/ShellDispatcher.php(66): ShellDispatcher->dispatch() #12 /home/scraper/public_html/ec_scraper/Vendor/cakephp/cakephp/lib/Cake/Console/cake.php(54): ShellDispatcher::run(Array) #13 {main}
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/11/02 00:45
2016/11/02 01:06 編集
2016/11/02 04:22
2016/11/02 04:28
2016/11/02 05:10
2016/11/02 05:13
2016/11/02 05:19
2016/11/02 06:00
2016/11/02 06:10
2016/11/02 06:50
2016/11/02 07:15 編集
2016/11/02 08:08
2016/11/02 13:06
2016/11/02 22:46
2016/11/04 01:00
2016/11/04 01:15
2016/11/04 01:29 編集
2016/11/04 01:53