目前,TP5.1官方已經提供了think-swoole2.0,集成程度之前優雅不少,不過5.0的集成方式確實有些雞肋。因此看了下2.0,爲5.0開發了一個擴展包,能夠採用composer下載php
composer require xaviertony/xavier-swoole
開發以前,須要先熟悉TP5.0的生命週期,否則就無從下手了。swoole
因爲TP主要在Apache或者NGINX下運行,每次運行結束都會進行釋放,而swoole則是常住內存,TP5不少類都由單例實現,因此不免會入坑,其中大坑主要是request,因爲啓動後請求被實例化,若是不刪除請求勢力,之後每次都是採用這個實例,形成沒法正常訪問頁面,由於每次請求達到後須要先將請求實例刪除app
public static function deletethis() { if (!is_null(self::$instance)) { self::$instance=null; } }
第三方包的配置文件必須在application/extra下,文件名爲swoole.phpcomposer
<?php return [ 'host' => '0.0.0.0', // 監聽地址 'port' => 9501, // 監聽端口 'mode' => '', // 運行模式 默認爲SWOOLE_PROCESS 'sock_type' => '', // sock type 默認爲SWOOLE_SOCK_TCP 'app_path' => getcwd() . '/application', // 應用地址 若是開啓了 'daemonize'=>true 必須設置(使用絕對路徑) 'file_monitor' => false, // 是否開啓PHP文件更改監控(調試模式下自動開啓) 'file_monitor_interval' => 2, // 文件變化監控檢測時間間隔(秒) 'file_monitor_path' => [], // 文件監控目錄 默認監控application和config目錄 // 能夠支持swoole的全部配置參數 'pid_file' => getcwd() . '/runtime/swoole.pid', 'log_file' => getcwd() . '/runtime/swoole.log', 'task_worker_num' => 20, //'document_root' => getcwd() . 'public', //'enable_static_handler' => true, 'daemonize' => 1,//守護 'worker_num' => 8, //worker process num 'max_request' => 10000, ];
啓動命令ui
php think swoole start
守護啓動this
php think swoole start -d
中止服務調試
php think swoole stop