$serv = new Swoole\Server('0.0.0.0', 9501, SWOOLE_BASE, SWOOLE_SOCK_TCP); $serv->set(array( 'worker_num' => 2, 'daemonize' => false, 'backlog' => 128, )); $serv->on('WorkerStart', function ($serv, $worker_id){ swoole_set_process_name("php swoole worker:" . $worker_id); $redis = new Redis(); $redis->pconnect('127.0.0.1', 6379); $redis->select(1);//監聽db1 $redis->setOption(\Redis::OPT_READ_TIMEOUT, -1); $redis->psubscribe(['__keyevent@1__:expired'], function ($redis, $pattern, $chan, $msg) use ($worker_id) { $orderId = intval($msg); if ($orderId % 2 == $worker_id){ echo "1Payload: $msg" . '@' . $worker_id . PHP_EOL; } }); }); $serv->on('receive', function ($serv, $fd, $reactor_id, $data) { echo "[#".$serv->worker_id."]\tClient[$fd]: $data\n"; }); $serv->start();