根据GatewayWorker文档中的描述:
DB属于单例模式,在onXXX里初始化的数据库单例只属于当前子进程自己所有;
那么我是不是可以如下理解:
<?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();
}
那么这样做的话,我就有一个问题了,如果这里我需要进行2个数据库实例怎么办?下面这么做我感觉是不正确的:
<?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();
}
大概知道这么解决这个问题了,不知道这么做对不对,请老大审阅:
对,
$worker->onWorkerStart代码不需要,可以删除。
Db::instance('db1')本身是单例的用法。