Skip to content

Commit

Permalink
完善子协程获取app对象
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Jun 7, 2021
1 parent a4026fa commit c22b53c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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扩展,然后使用
Expand All @@ -22,15 +23,5 @@ php think swoole

swoole的相关参数可以在`config/swoole.php`里面配置(具体参考配置文件内容)。

如果需要使用守护进程方式运行,可以配置
如果需要使用守护进程方式运行,建议使用supervisor来管理进程

~~~
'options' => [
'daemonize' => true
]
~~~

支持的操作包括
~~~
php think swoole [start|stop|reload|restart]
~~~
24 changes: 19 additions & 5 deletions src/Sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InvalidArgumentException;
use ReflectionObject;
use RuntimeException;
use Swoole\Coroutine;
use think\App;
use think\Config;
use think\Container;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down
9 changes: 4 additions & 5 deletions src/concerns/InteractsWithServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace think\swoole\concerns;

use Closure;
use Swoole\Constant;
use Swoole\Coroutine;
use Swoole\Coroutine\Barrier;
Expand Down Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/coroutine/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down

0 comments on commit c22b53c

Please sign in to comment.