From c22b53c50b4ea7b309a4f7b2c50698f9872d124a Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Mon, 7 Jun 2021 19:45:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=AD=90=E5=8D=8F=E7=A8=8B?= =?UTF-8?q?=E8=8E=B7=E5=8F=96app=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 ++----------- src/Sandbox.php | 24 +++++++++++++++++++----- src/concerns/InteractsWithServer.php | 9 ++++----- src/coroutine/Context.php | 6 +++--- 4 files changed, 28 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index c8789b1..f1d8ac1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ ThinkPHP Swoole 扩展 =============== +交流群:787100169 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png "点击加群")](https://jq.qq.com/?_wv=1027&k=VRcdnUKL) ## 安装 首先按照Swoole官网说明安装swoole扩展,然后使用 @@ -22,15 +23,5 @@ php think swoole swoole的相关参数可以在`config/swoole.php`里面配置(具体参考配置文件内容)。 -如果需要使用守护进程方式运行,可以配置 +如果需要使用守护进程方式运行,建议使用supervisor来管理进程 -~~~ -'options' => [ - 'daemonize' => true -] -~~~ - -支持的操作包括 -~~~ -php think swoole [start|stop|reload|restart] -~~~ \ No newline at end of file diff --git a/src/Sandbox.php b/src/Sandbox.php index 5cd74e9..86e4212 100644 --- a/src/Sandbox.php +++ b/src/Sandbox.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use ReflectionObject; use RuntimeException; +use Swoole\Coroutine; use think\App; use think\Config; use think\Container; @@ -112,7 +113,7 @@ public function clear() public function getApplication($init = false) { - $snapshot = $this->getSnapshot(); + $snapshot = $this->getSnapshot($init); if ($snapshot instanceof Container) { return $snapshot; } @@ -126,18 +127,31 @@ public function getApplication($init = false) throw new InvalidArgumentException('The app object has not been initialized'); } - protected function getSnapshotId() + protected function getSnapshotId($init = false) { - return Context::getCoroutineId(); + if ($init) { + Coroutine::getContext()->offsetSet('#root', true); + return Coroutine::getCid(); + } else { + $cid = Coroutine::getCid(); + while (!Coroutine::getContext($cid)->offsetExists('#root')) { + $cid = Coroutine::getPcid($cid); + if ($cid < 1) { + break; + } + } + + return $cid; + } } /** * Get current snapshot. * @return App|null */ - public function getSnapshot() + public function getSnapshot($init = false) { - return $this->snapshots[$this->getSnapshotId()] ?? null; + return $this->snapshots[$this->getSnapshotId($init)] ?? null; } public function setSnapshot(Container $snapshot) diff --git a/src/concerns/InteractsWithServer.php b/src/concerns/InteractsWithServer.php index e1cb594..91fb640 100644 --- a/src/concerns/InteractsWithServer.php +++ b/src/concerns/InteractsWithServer.php @@ -2,7 +2,6 @@ namespace think\swoole\concerns; -use Closure; use Swoole\Constant; use Swoole\Coroutine; use Swoole\Coroutine\Barrier; @@ -119,12 +118,12 @@ public function sendMessage($workerId, $message) } } - public function runWithBarrier(Closure $callable) + public function runWithBarrier(callable $func, ...$params) { $barrier = Barrier::make(); - Coroutine::create(function () use ($callable, $barrier) { - $callable(); - }); + Coroutine::create(function (...$params) use ($func, $barrier) { + $func(...$params); + }, ...$params); Barrier::wait($barrier); } diff --git a/src/coroutine/Context.php b/src/coroutine/Context.php index 2294ab8..f16881e 100644 --- a/src/coroutine/Context.php +++ b/src/coroutine/Context.php @@ -22,10 +22,10 @@ public static function get($cid = null) public static function getDataObject() { $context = self::get(); - if (!isset($context['data'])) { - $context['data'] = new ArrayObject(); + if (!isset($context['#data'])) { + $context['#data'] = new ArrayObject(); } - return $context['data']; + return $context['#data']; } /**