table)->count();
echo "共有 {$total} 条记录需要更新", PHP_EOL;
if ($total <= 0) {
echo '没有需要更新的记录', PHP_EOL;
return;
}
// 分割数据
$pageSize = ceil($total / $this->processNum);
// 设置 Swoole 运行时参数
Runtime::setHookFlags(SWOOLE_HOOK_ALL);
Coroutine::create(function () use ($pageSize) {
for ($i = 0; $i < $this->processNum; $i++) {
$start = $pageSize * $i;
Coroutine::create(function () use ($start) {
while (true) {
$users = Db::name($this->table)
->where('id', '>=', $start)
->limit($this->batchSize)
->select();
if (empty($users)) {
break;
}
$ids = [];
foreach ($users as $user) {
$ids[] = $user['id'];
// 更新用户信息
Db::name($this->table)->where('id', $user['id'])->update(['is_updated' => 1]);
}
$msg = '[' . date('Y-m-d H:i:s') . '] ' . implode(',', $ids) . "\n";
file_put_contents('update.log', $msg, FILE_APPEND);
$start += $this->batchSize;
}
});
}
});
Coroutine::wait();
$endTime = microtime(true);
echo 'Swoole 协程方式更新用户信息完成,用时 ' . round($endTime - $startTime, 4) . ' 秒。', PHP_EOL;
}
}
本文共 个字数,平均阅读时长 ≈ 分钟,您已阅读:0时0分0秒。
649494848