本文小编为大家详细介绍“thinkphp6中如何使用workerman”,内容详细,步骤清晰,细节处理妥当,希望这篇“thinkphp6中如何使用workerman”文免费云主机域名章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。thinkphp6整合workerman教程thinkphp6安装workerman命令:
composerrequiretopthink/think-worker
第一步,创建一个自定义命令类文件,运行指令。
phpthinkmake:commandSpiderspider
会生成一个appcommandSpider
命令行指令类,我们修改内容如下:
setName('spider') ->addArgument('status',Argument::OPTIONAL,"status") ->addArgument('controller_name',Argument::OPTIONAL,"controller_name/controller_name") ->addArgument('mode',Argument::OPTIONAL,"d") ->setDescription('spidercontrol'); /** *以上设置命令格式为:phpthinkspider[status][controller_name/controller_name][d] *think为thinkphp框架入口文件 *spider为在框架中注册的命令,上面setName设置的 *staus为workerman框架接受的命令 *controller_name/controller_name为控制器名称,以正斜线分割,执行制定控制器,为空或缺省则启动所有控制器,控制器列表在controller_name属性中注册 *d最后一个参数为wokerman支持的-d-g参数,但是不用加-,直接使用d或者g *phpthinkspiderstartcollect/SendMsg */ } /** *execute *tp框架自定义指令特性 *执行命令后的逻辑 *@parammixed$input *@parammixed$output *@returnmixed */ protectedfunctionexecute(Input$input,Output$output) { //获得status参数,即think自定义指令中的第一个参数,缺省报错 $status=$input->getArgument('status'); if(!$status){ $output->writeln('pelaseinputcontrolcommand,likestart'); exit; } //获得控制器名称 $controller_str=$input->getArgument('controller_name'); //获得模式,d为wokerman的后台模式(生产环境) $mode=$input->getArgument('mode'); //分析控制器参数,如果缺省或为all,那么运行所有注册的控制器 $controller_list=$this->controller_names; if($controller_str!=''&&$controller_str!='all') { $controller_list=explode('/',$controller_str); } //重写mode参数,改为wokerman接受的参数 if($mode=='d'){ $mode='-d'; } if($mode=='g'){ $mode='-g'; } //将wokerman需要的参数传入到其parseCommand方法中,此方法在start类中重写 Start::$argvs=[ 'think', $status, $mode ]; $output->writeln('startrunningspider'); $programs_ob_list=[]; //实例化需要运行的控制器 foreach($controller_listas$c_key=>$controller_name){ $class_name='app'.$this->model_name.'controller'.$controller_name; $programs_ob_list[]=new$class_name(); } //将控制器的相关回调参数传到workerman中 foreach(['onWorkerStart','onConnect','onMessage','onClose','onError','onBufferFull','onBufferDrain','onWorkerStop','onWorkerReload']as$event){ foreach($programs_ob_listas$p_key=>$program_ob){ if(method_exists($program_ob,$event)){ $programs_ob_list[$p_key]->$event=[$program_ob,$event]; } } } Start::runAll(); } }
例如我们创建一个定时器的命令appservercontroller创建WebhookTimer.php
执行start命令行
phpthinkspiderstart执行stop命令
phpthinkspiderstop执行全部进程命令
phpthinkspiderstartalld在
appcommandSpider.php文件
public $controller_names = [‘WebhookTimer’,’其他方法’,’其他方法’];其他方法 就是appservercontroller下创建的其他类文件方法
完结Start.php文件socket?:$this->protocol.'://'.$this->host.':'.$this->port,$this->context); } /** *parseCommand *重写wokerman的解析命令方法 *@returnmixed */ publicstaticfunctionparseCommand() { if(static::$_OS!==OS_TYPE_LINUX){ return; } //static::$argvs; //Checkstatic::$argvs; $start_file=static::$argvs[0]; $available_commands=array( 'start', 'stop', 'restart', 'reload', 'status', 'connections', ); $usage="Usage:phpyourfile[mode]nCommands:nstartttStartworkerinDEBUGmode.nttUsemode-dtostartinDAEMONmode.nstopttStopworker.nttUsemode-gtostopgracefully.nrestartttRestartworkers.nttUsemode-dtostartinDAEMONmode.nttUsemode-gtostopgracefully.nreloadttReloadcodes.nttUsemode-gtoreloadgracefully.nstatusttGetworkerstatus.nttUsemode-dtoshowlivestatus.nconnectionstGetworkerconnections.n"; if(!isset(static::$argvs[1])||!in_array(static::$argvs[1],$available_commands)){ if(isset(static::$argvs[1])){ static::safeEcho('Unknowncommand:'.static::$argvs[1]."n"); } exit($usage); } //Getcommand. $command=trim(static::$argvs[1]); $command2=isset(static::$argvs[2])?static::$argvs[2]:''; //Startcommand. $mode=''; if($command==='start'){ if($command2==='-d'||static::$daemonize){ $mode='inDAEMONmode'; }else{ $mode='inDEBUGmode'; } } static::log("Workerman[$start_file]$command$mode"); //GetmasterprocessPID. $master_pid=is_file(static::$pidFile)?file_get_contents(static::$pidFile):0; $master_is_alive=$master_pid&&posix_kill($master_pid,0)&&posix_getpid()!=$master_pid; //Masterisstillalive? if($master_is_alive){ if($command==='start'){ static::log("Workerman[$start_file]alreadyrunning"); exit; } }elseif($command!=='start'&&$command!=='restart'){ static::log("Workerman[$start_file]notrun"); exit; } //executecommand. switch($command){ case'start': if($command2==='-d'){ static::$daemonize=true; } break; case'status': while(1){ if(is_file(static::$_statisticsFile)){ @unlink(static::$_statisticsFile); } //MasterprocesswillsendSIGUSR2signaltoallchildprocesses. posix_kill($master_pid,SIGUSR2); //Sleep1second. sleep(1); //Clearterminal. if($command2==='-d'){ static::safeEcho("33[H33[2J33(B33[m",true); } //Echostatusdata. static::safeEcho(static::formatStatusData()); if($command2!=='-d'){ exit(0); } static::safeEcho("nPressCtrl+Ctoquit.nn"); } exit(0); case'connections': if(is_file(static::$_statisticsFile)&&is_writable(static::$_statisticsFile)){ unlink(static::$_statisticsFile); } //MasterprocesswillsendSIGIOsignaltoallchildprocesses. posix_kill($master_pid,SIGIO); //Waitingamoment. usleep(500000); //Displaystatisitcsdatafromadiskfile. if(is_readable(static::$_statisticsFile)){ readfile(static::$_statisticsFile); } exit(0); case'restart': case'stop': if($command2==='-g'){ static::$_gracefulStop=true; $sig=SIGTERM; static::log("Workerman[$start_file]isgracefullystopping..."); }else{ static::$_gracefulStop=false; $sig=SIGINT; static::log("Workerman[$start_file]isstopping..."); } //Sendstopsignaltomasterprocess. $master_pid&&posix_kill($master_pid,$sig); //Timeout. $timeout=5; $start_time=time(); //Checkmasterprocessisstillalive? while(1){ $master_is_alive=$master_pid&&posix_kill($master_pid,0); if($master_is_alive){ //Timeout? if(!static::$_gracefulStop&&time()-$start_time>=$timeout){ static::log("Workerman[$start_file]stopfail"); exit; } //Waitingamoment. usleep(10000); continue; } //Stopsuccess. static::log("Workerman[$start_file]stopsuccess"); if($command==='stop'){ exit(0); } if($command2==='-d'){ static::$daemonize=true; } break; } break; case'reload': if($command2==='-g'){ $sig=SIGQUIT; }else{ $sig=SIGUSR1; } posix_kill($master_pid,$sig); exit; default: if(isset($command)){ static::safeEcho('Unknowncommand:'.$command."n"); } exit($usage); } } } 读到这里,这篇“thinkphp6中如何使用workerman”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注百云主机行业资讯频道。
本篇内容介绍了“thinkphp缓存怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! thinkphp缓存的用法:1、使用模型查询,其代码语句如“$dat…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。