swoole提供一個編譯好的二進制包,這個包連php都包含進去了,下載解壓後就能夠直接運行,都不用安裝phpphp
在這個地方直接下載二進制包ubuntu
https://www.swoole.com/page/downloadswoole
下載完按照官方建議解壓縮到/opt/swoole tar -xvf swoole.tar.xz -C /opt/swoolespa
直接寫一個php文件使用swoole目錄裏的php進行運行code
<?php $serv = new Swoole\Server("127.0.0.1", 9502, SWOOLE_BASE); $serv->set(array( 'worker_num' => 2, 'task_worker_num' => 4, )); $serv->on('Receive', function(Swoole\Server $serv, $fd, $from_id, $data) { echo "接收數據" . $data . "\n"; $data = trim($data); $task_id = $serv->task($data, 0); $serv->send($fd, "分發任務,任務id爲$task_id\n"); }); $serv->on('Task', function (Swoole\Server $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 (Swoole\Server $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.phpblog