Swoole是PHP的异步、并行、高性能网络通讯引擎,使用纯C语言编写,提供了PHP语言的异步多线程服务器,异步TCP/UDP网络客户端,异步MySQL,异步Redis,数据库链接池,AsyncTask,消息队列,毫秒定时器,异步文件读写,异步DNS查询。 Swoole内置了Http/WebSocket服务器端/客户端、Http2.0服务器端/客户端。php
上面只是官方概念!数据库
我在项目中主要使用Swoole拓展实现消息队列,挺高任务的执行效率。服务器
框架环境:ThinkPHP 5.0网络
第一步,配置command.php文件,目录在application/command.php。多线程
return [ 'app\console\command\Test', ];
第二步,创建命令类文件,新建application/console/command/Test.php。app
<?php /** * Created by PhpStorm. * User: 小良 * Date: 2017/7/24 * Time: 10:55 */ namespace app\console\command; use think\console\Command; use think\console\Input; use think\console\input\Argument; use think\console\input\Option; use think\console\Output; class Test extends Command{ /** * 定义命令 * 命令名称是 test */ protected function configure() { //设置参数 $this->addArgument('email', Argument::REQUIRED); //必传参数 $this->addArgument('mobile', Argument::OPTIONAL);//可选参数 //选项定义 $this->addOption('message', 'm', Option::VALUE_REQUIRED, 'test'); //选项值必填 $this->addOption('status', 's', Option::VALUE_OPTIONAL, 'test'); //选项值选填 $this->setName('test')->setDescription('Here is the remark '); } /** * 命令执行的内容 * @param Input $input * @param Output $output */ protected function execute(Input $input, Output $output) { //获取参数值 $args = $input->getArguments(); $output->writeln('The args value is:'); print_r($args); //获取选项值 $options = $input->getOptions(); $output->writeln('The options value is:'); print_r($options); $output->writeln("TestCommand:"); $output->writeln("End.."); } }
第三部,在框架的目录下面运行命令框架
php think test email mobile -m"mtset" -s"stest"
至此,自定义命令行已经完成!异步