Skip to content

Commit

Permalink
V4.0.x (#43)
Browse files Browse the repository at this point in the history
* feat: swoole version >=4.4.4可以不用全局禁用协程

* feat: 兼容swoole高版本,不需要强制关闭协程
  • Loading branch information
kcloze authored Nov 28, 2019
1 parent 41d9943 commit 7e0d3f0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ return PhpCsFixer\Config::create()
'combine_consecutive_unsets' => true,
// one should use PHPUnit methods to set up expected exception instead of annotations
'general_phpdoc_annotation_remove' => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'],
'header_comment' => ['header' => $header],
//'header_comment' => ['header' => $header],
//'heredoc_to_nowdoc' => true,
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
'no_unreachable_default_argument_value' => true,
Expand Down
4 changes: 2 additions & 2 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ protected function sendSignalHttpServer($signal=SIGTERM)

private function checkSwooleSetting()
{
if (version_compare(swoole_version(), '4.0.0', '>=') && 'Off' !== ini_get('swoole.enable_coroutine')) {
$this->output->writeln('swoole version >=4.0.0,you have to disable coroutine in php.ini');
if (version_compare(swoole_version(), '4.4.4', '<=') && version_compare(swoole_version(), '4.0.0', '>=') && 'Off' !== ini_get('swoole.enable_coroutine')) {
$this->output->writeln('4.0.0 <= swoole version <=4.4.4,you have to disable coroutine in php.ini, or you can update swoole version >=4.4.4');
$this->output->writeln('details jump to: https://github.com/swoole/swoole-src/issues/2716');
exit;
}
Expand Down
22 changes: 14 additions & 8 deletions src/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ public function start()
*/
public function reserveQueue($num, $topic, $type=self::CHILD_PROCESS_CAN_RESTART)
{
$this->disableCoroutine();
$reserveProcess = new \Swoole\Process(function ($worker) use ($num, $topic, $type) {
$this->checkMpid($worker);
$beginTime=microtime(true);
Expand All @@ -156,6 +155,9 @@ public function reserveQueue($num, $topic, $type=self::CHILD_PROCESS_CAN_RESTART
$this->logger->log($type . ' ' . $topic . ' worker id: ' . $num . ', pid: ' . getmypid() . ' is done!!! popNum: ' . $jobs->popNum . ', Timing: ' . ($endTime - $beginTime), 'info', $this->logSaveFileWorker);
unset($num, $topic, $type);
});

$this->disableCoroutine($reserveProcess);

$pid = $reserveProcess->start();
$this->workers[$pid] = $reserveProcess;
$this->workersInfo[$pid]['type'] = $type;
Expand Down Expand Up @@ -439,13 +441,17 @@ private function showStatus()
return $statusStr;
}

private function disableCoroutine()
private function disableCoroutine(\Swoole\Process $reserveProcess=null)
{
//swoole 4 下只能通过php.ini 全局关闭协程,官方回复 https://github.com/swoole/swoole-src/issues/2716

// if (version_compare(swoole_version(), '4.0.0', '>=')) {
// $this->logger->log("Swoole Version >= 4.0.0 ,disable coroutine.", 'info', $this->logSaveFileWorker);
// ini_set('swoole.enable_coroutine', 'Off');
// }
//swoole 4.4.4
if (version_compare(swoole_version(), '4.4.4', '>=')) {
if($reserveProcess instanceof \Swoole\Process){
$reserveProcess->set(['enable_coroutine' => false]);
}
\Swoole\Timer::set([
'enable_coroutine' => false,
]);
$this->logger->log('Swoole Version >= 4.4.4 ,disable coroutine.', 'info', $this->logSaveFileWorker);
}
}
}

0 comments on commit 7e0d3f0

Please sign in to comment.