简单Swoole异步列队操作

class swooleController extends Mb{
//__init 函数会在控制器被创建时自动运行用于初始化工作,如果您要使用它,请按照以下格式编写代码即可:
public $swoole;
public function index(){
$this->curl = t('curl');
$this->swoole = t('swooleAsync',$this);
$res = $this->swoole->start();
if($res){
echo '服务启动成功';
}else{
echo '服务启动失败';
}
}
public function __Task($task_id,$data){//此方法函数必须存在
$dataArr = json_decode($data,true);
var_dump($dataArr);
$dbres = db('order_list')->where('id = ?',[$dataArr['id']])->fetch('state');
if($dbres){
$res = $this->curl->get('http://www.liot.cn/async/test?state='.$dbres['state']);
echo $res."\n";
return $res;
}
return "任务ID:{$task_id},执行ID不存在";
}
public function __Finish($task_id,$data){//此方法函数必须存在
echo "异步任务ID:{$task_id} 完成: {$data}".PHP_EOL;
}
}客户端:class indexController extends Mb{
public function index(){
$this->swoole = t('swooleAsync');
$res = $this->swoole->start(1);
if($res){
$this->swoole->send(['id'=>rand(1,100)]);
}
$this->swoole->close();
echo 'ok';
}
public function test(){
sleep(rand(1,5));
if(isset($_GET['state']) && $_GET['state'] == 2){
$this->json('成功',200);
}$this->json('失败',201);
}
}启动服务端:/**
* swoole服务入口
* @link http://www.ZOSphp.com
* @copyright Copyright (c) 2022-2032
* @author 友易
* @version 1.0.0
**/
//define('Z_DEBUG',TRUE); // 开启调试模式
$_GET['s'] = 'swoole/index';
include 'Mb/Mb.php';

可以赞助并支持下作者哦,谢谢!