根據(jù)GatewayWorker文檔中的描述:
DB屬于單例模式,在onXXX里初始化的數(shù)據(jù)庫(kù)單例只屬于當(dāng)前子進(jìn)程自己所有;
那么我是不是可以如下理解:
<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->onWorkerStart = function()
{
Db::instance('db1');
};
$worker->onMessage = function()
{
$ret = Db->select('name,age')->from('users')->where('age>12')->query();
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
那么這樣做的話,我就有一個(gè)問(wèn)題了,如果這里我需要進(jìn)行2個(gè)數(shù)據(jù)庫(kù)實(shí)例怎么辦?下面這么做我感覺(jué)是不正確的:
<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$db1 = null;
$db2 = null;
$worker->onWorkerStart = function()
{
$db1 = Db::instance('db1');
$db2 = Db::instance('db2');
};
$worker->onMessage = function()
{
$ret1 = $db1->select('name,age')->from('users')->where('age>12')->query();
$ret2 = $db2->select('name,age')->from('users')->where('age>12')->query();
}
if(!defined('GLOBAL_START'))
{
Worker::runAll();
}
大概知道這么解決這個(gè)問(wèn)題了,不知道這么做對(duì)不對(duì),請(qǐng)老大審閱:
<?php
$worker = new BusinessWorker();
$worker->name = 'YourAppBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->onWorkerStart = function()
{
Db::instance('db1');
Db::instance('db2');
};
$worker->onMessage = function()
{
Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();
Db::instance('db1')->select('name,age')->from('users')->where('age>12')->query();
}