1、环境准备
php7.3/8+
swoole4.6+
2、示例代码
Coroutine\run(function () use ($db_work, $db_process) {
$connPool = new PDOPool((new PDOConfig)
->withHost($db_work['host'])
->withPort(3306)
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName($db_work['database'])
->withCharset('utf8mb4')
->withUsername($db_work['user'])
->withPassword($db_work['password'])
);
$processPool = new PDOPool((new PDOConfig)
->withHost($db_process['host'])
->withPort(3306)
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName($db_process['database'])
->withCharset('utf8mb4')
->withUsername($db_process['user'])
->withPassword($db_process['password'])
);
$channel = new Channel(60);
$conn = $connPool->get();
$data1 = $conn->query('SELECT count(*) FROM `goods_images` WHERE image_url!=\'\'')->fetchAll(PDO::FETCH_NUM);
$data2 = $conn->query('SELECT count(*) FROM `goods_sku` WHERE original_img!=\'\' AND is_delete=0')->fetchAll(PDO::FETCH_NUM);
$data3 = $conn->query('SELECT count(*) FROM `goods_spu` WHERE original_img!=\'\' AND is_delete=0')->fetchAll(PDO::FETCH_NUM);
$connPool->put($conn);
$goods_images_total = $data1[0][0];
$goods_sku_total = $data2[0][0];
$goods_spu_total = $data3[0][0];
$todo = [
'goods_images' => $goods_images_total,
'goods_sku' => $goods_sku_total,
'goods_spu' => $goods_spu_total,
];
print_r($todo);
Coroutine::create(function () use ($channel, $todo) {
foreach ($todo as $table => $total) {
$page = 0; //第一页开始
while ($page++ <= $total) {
$channel->push(['page' => $page, 'table' => $table]);
}
}
});
Coroutine::create(function () use ($channel, $connPool, $processPool) {
while (1) {
$data = $channel->pop(10);
if ($data) {
$page = $data['page'];
$table = $data['table'];
// todo
echo($page . ' <== task执行完毕 ok' . PHP_EOL);
} else {
if ($channel->isEmpty()) {
echo 'images $channel:empty' . PHP_EOL;
break;
}
}
}
});
});
a.分析建立可以独立运行的最小单位cell
中间异步瓶颈mysql也使用连接池,连接池容量应该大于协程任务池容量;使用mysql连接池注意“有借有还”,$conn = $connPool->get();$connPool->put($conn);
b.队列化需求参数,使用channel.push发送参数
加速执行如秒杀处理,应当使用队列。
c.使用channel.pop接收参数,供给cell执行
协程池添加-取用
保持一致
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。