swoole提供一个编译好的二进制包,这个包连php都包含进去了,下载解压后就可以直接运行,都不用安装php
在这个地方直接下载二进制包
https://www.swoole.com/page/download
下载完按照官方建议解压缩到/opt/swoole tar -xvf swoole.tar.xz -C /opt/swoole
直接写一个php文件使用swoole目录里的php进行运行
代码语言:javascript复制<?php
$serv = new SwooleServer("127.0.0.1", 9502, SWOOLE_BASE);
$serv->set(array(
'worker_num' => 2,
'task_worker_num' => 4,
));
$serv->on('Receive', function(SwooleServer $serv, $fd, $from_id, $data) {
echo "接收数据" . $data . "n";
$data = trim($data);
$task_id = $serv->task($data, 0);
$serv->send($fd, "分发任务,任务id为$task_idn");
});
$serv->on('Task', function (SwooleServer $serv, $task_id, $from_id, $data) {
echo "Tasker进程接收到数据";
echo "#{$serv->worker_id}tonTask: [PID={$serv->worker_pid}]: task_id=$task_id, data_len=".strlen($data).".".PHP_EOL;
$serv->finish($data);
});
$serv->on('Finish', function (SwooleServer $serv, $task_id, $data) {
echo "Task#$task_id finished, data_len=".strlen($data).PHP_EOL;
});
$serv->on('workerStart', function($serv, $worker_id) {
global $argv;
if($worker_id >= $serv->setting['worker_num']) {
swoole_set_process_name("php {$argv[0]}: task_worker");
} else {
swoole_set_process_name("php {$argv[0]}: worker");
}
});
$serv->start();
/opt/swoole/bin/php /mnt/f/ubuntu/phpProject/myworker/task.php