PHP code example of mevyen / yii2-swoole-async

1. Go to this page and download the library: Download mevyen/yii2-swoole-async library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mevyen / yii2-swoole-async example snippets


return [
    'swooleAsync' => [
		'host'             => '127.0.0.1', 		//服务启动IP
		'port'             => '9512',      		//服务启动端口
		'process_name'     => 'swooleServ',		//服务进程名
		'open_tcp_nodelay' => '1',         		//启用open_tcp_nodelay
		'daemonize'        => '1',				//守护进程化
		'worker_num'       => '2',				//work进程数目
		'task_worker_num'  => '2',				//task进程的数量
		'task_max_request' => '10000',			//work进程最大处理的请求数
		'task_tmpdir'      => '/tmp/task',		 //设置task的数据临时目录
		'log_file'         => '/tmp/log/http.log', //指定swoole错误日志文件
		'client_timeout'   => '20',				 //client链接服务器时超时时间(s)
		'pidfile'          => '/tmp/y.pid', 		 //服务启动进程id文件保存位置

		//--以上配置项均来自swoole-server的同名配置,可随意参考swoole-server配置说明自主增删--
		'log_size'         => 204800000, 			 //运行时日志 单个文件大小
		'log_dir'          => '/tmp/log',			 //运行时日志 存放目录
	]
];

'controllerMap' => [
    'swooleasync' => [
        'class' => 'mevyen\swooleAsync\SwooleAsyncController',
    ],
],

'components' => [
    'swooleasync' => [
        'class' => 'mevyen\swooleAsync\SwooleAsyncComponent',
    ]
]

//启动
/path/to/yii/application/yii swooleasync/run start
 
//重启
/path/to/yii/application/yii swooleasync/run restart

//停止
/path/to/yii/application/yii swooleasync/run stop

//查看状态
/path/to/yii/application/yii swooleasync/run stats

//查看进程列表
/path/to/yii/application/yii swooleasync/run list


namespace console\controllers;
use yii\console\Controller;

class TestController extends Controller 
{  
	public function actionSwooleasync(){
		$data = [
			"data":[
				[
					"a" => "test/mail",
					"p" => ["测试邮件1","测试邮件2"]
				],
				...
			],
			"finish" => [
				[
					"a" => "test/mail",
					"p" => ["测试邮件回调1","测试邮件回调2"]
				],
				...
			]
		];
		\Yii::$app->swooleasync->async(json_encode($data));
	}

	public function actionMail($a='',$b=''){
		echo $a.'-'.$b;
	}  
}

* * * * * /path/to/yii/application/yii swooleasync/run start >> /var/log/console-app.log 2>&1