swoft自定义进程

bean.php中添加一下配置

'processPool' => [
    // 进程处理类,必填
    'class' => \Swoft\Process\ProcessPool::class,
    
    // 进程数,进程id从排序,必填
    'workerNum' => 3,
    // IPC类型,进程间通信类型,参数参考swoole,选填
    'ipcType' => SWOOLE_IPC_NONE
    // 是否开启协程,默认是开启,选填
    'coroutine'  => true
]

进程处理文件位于App/Process

必须继承ProcessInterface
使用注释@Process标记
workerId=0workerId={1,2}选择进程id,id从0开始,可以安排多个进程;workerId 如果不写默认情况,当前程序流程绑定到其它未绑定的进程

Worker1Process.php示例

<?php declare(strict_types=1);
/**
 * This file is part of Swoft.
 *
 * @link     https://swoft.org
 * @document https://swoft.org/docs
 * @contact  group@swoft.org
 * @license  https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Process;

use Swoft\Amqp\Amqp;
use Swoft\Log\Helper\CLog;
use Swoft\Process\Annotation\Mapping\Process;
use Swoft\Process\Contract\ProcessInterface;
use Swoole\Coroutine;
use Swoole\Process\Pool;

/**
 * Class Worker1Process
 *
 * @since 2.0
 *
 * @Process(workerId=0)
 */
class Worker1Process implements ProcessInterface
{
    /**
     * @param Pool $pool
     * @param int  $workerId
     */
    public function run(Pool $pool, int $workerId): void
    {
        while (true) {
            CLog::info('worker-' . $workerId);

            CLog::info('run method: '.__METHOD__);
            $channel = Amqp::channel('channel_1');
            $channel->listen(function ($message){
                //$message:数据结构(json_encode)之后
                //{"body":"hey!-----9","body_size":10,"is_truncated":false,"content_encoding":null,"delivery_info":{"channel":{"callbacks":{"amq.ctag-epGZgfHej3YrjZk2FBvp0A":{}}},"delivery_tag":99,"redelivered":false,"exchange":"exchange_test","routing_key":"example-test-routing-key","consumer_tag":"amq.ctag-epGZgfHej3YrjZk2FBvp0A"}}
                CLog::info('message:'. json_encode($message));

            });

            $name = 'steve';
            vdump(context()->getResponse()->withContent('Hello' . ($name === '' ? '' : ", {$name}")));
            Coroutine::sleep(3);

        }
    }
}

进程单独启动

// 启动
php bin/swoft process:start
// 后台
php bin/swoft process:start -d
// 重启,进程池全部重启
php bin/swoft process:reload
// 停止,进程池全部停止
php bin/swoft process:stop
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不知飞到哪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值