webman2.1支持了協(xié)程,文檔里數(shù)據(jù)庫這節(jié),寫到“每個(gè)進(jìn)程有自己的連接池,進(jìn)程間不共享連接池?!?/p>
webman 1版本時(shí),數(shù)據(jù)庫啟動是在onWorkerStart里里加載LaravelDb.php,從而啟動數(shù)據(jù)庫,每個(gè)進(jìn)程一個(gè)數(shù)據(jù)庫連接。
1、請教社區(qū)大牛,2.1版本是怎么做到每個(gè)進(jìn)程有自己的連接池,進(jìn)程間不共享連接池的?源碼看不懂,有沒有大牛幫忙解析分析一下關(guān)鍵代碼。
2、文檔提到,當(dāng)使用Swoole Swow Fiber驅(qū)動時(shí),workerman每次運(yùn)行onWorkerStart onMessage onConnect onClose等回調(diào)時(shí)會自動創(chuàng)建一個(gè)協(xié)程來執(zhí)行。源碼中Worker.php中
public function run(): void
{
$this->listen();
if (!$this->onWorkerStart) {
return;
}
// Try to emit onWorkerStart callback.
$callback = function() {
try {
($this->onWorkerStart)($this);
} catch (Throwable $e) {
// Avoid rapid infinite loop exit.
sleep(1);
static::stopAll(250, $e);
}
};
switch (Worker::$eventLoopClass) {
case Swoole::class:
case Swow::class:
case Fiber::class:
Coroutine::create($callback);
break;
default:
(new \Fiber($callback))->start();
}
}
只有run方法中的onWorkerStart回調(diào)中新建了協(xié)程,沒有看到其他回調(diào)中新建協(xié)程,請大佬指教?。?!
謝謝?。?!
你可以這么理解,每個(gè)進(jìn)程的數(shù)據(jù)是隔離的,除非你使用Workbunny\WebmanSharedCache\Cache; 之類的共享緩存。
所以你應(yīng)該考慮下PHP多進(jìn)程下所有數(shù)據(jù)都是共享的會不會出問題。