yield代码还会执行吗(Yii2 queue的队列使用详解)
类别:编程学习 浏览量:643
时间:2022-01-16 00:37:23 yield代码还会执行吗
Yii2 queue的队列使用详解少废话主要看文档
官方文档
yii2-queue 的使用
1.安装
|
composer require --prefer-dist yiisoft/yii2-queue |
2.配置,在 common/config/main.php 中配置
redis作为驱动
|
return [ 'bootstrap' => [ 'queue' , // 把这个组件注册到控制台 ], 'components' => [ 'redis' => [ 'class' => \yii\redis\Connection:: class , // ... ], 'queue' => [ 'class' => \yii\queue\redis\Queue:: class , 'as log' => \yii\queue\LogBehavior:: class , //错误日志 默认为 console/runtime/logs/app.log 'redis' => 'redis' , // 连接组件或它的配置 'channel' => 'queue' , // Queue channel key ], ], ]; |
File 作为驱动
|
return [ 'bootstrap' => [ 'queue' , // 把这个组件注册到控制台 ], 'components' => [ 'queue' => [ 'class' => \yii\queue\file\Queue:: class , 'as log' => \yii\queue\LogBehavior:: class , //错误日志 默认为 console/runtime/logs/app.log 'path' => '@runtime/queue' , ], ], ]; |
3.新建 frontend/components/DownloadJob
|
class DownloadJob extends BaseObject implements \yii\queue\JobInterface { public $url ; public $file ; public function execute( $queue ) { file_put_contents ( $this ->file, file_get_contents ( $this ->url)); } } |
4.控制台
控制台用于监听和处理队列任务。
cmd 下 监听队列
|
yii queue/listen |
5.添加到队列
将任务添加到队列:
|
Yii:: $app ->queue->push( new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg' , 'file' => '/tmp/image.jpg' , ])); |
将任务推送到队列中延时5分钟运行:
|
Yii:: $app ->queue->delay(5 * 60)->push( new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg' , 'file' => '/tmp/image.jpg' , ])); |
6.测试
执行 5 中的程序,控制台监听到,便会后台自动 下载http://example.com/image.jpg到本地为/tmp/image.jpg
启动worker
可以使用Supervisor或Systemd 来启动多进程worker,也可以使用 Cron,我们这里主要说一下Supervisor
centos7 supervisor的使用
1.安装supervisor
|
yum update yum install epel-release yum install -y supervisor #开机启动 systemctl enable supervisord #启动 systemctl start supervisord |
2.supervisor 命令
|
supervisorctl status 查看进程状态 supervisorctl reload 重启supervisord supervisorctl start|stop|restart 启动关闭重启进程 |
3.添加配置文件
Supervisor 配置文件通常在 /etc/supervisord.d 目录下. 你可以创建一些配置文件在这里.
注:文件名是.ini结尾
下面就是个例子:
|
[program:yii-queue-worker] process_name=%(program_name)s_%(process_num)02d command=/usr/bin/php /var/www/my_project/yii queue/listen --verbose=1 --color=0 autostart=true autorestart=true user=www-data numprocs=4 redirect_stderr=true stdout_logfile=/var/www/my_project/log/yii-queue-worker.log |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://segmentfault.com/a/1190000019805030
您可能感兴趣
- python怎么安装queue(python队列Queue的详解)
- python栈和队列(Python 实现数据结构中的栈队列)
- python 队列与列表(Python实现的栈、队列、文件目录遍历操作示例)
- laravel队列大批量数据(laravel 中某一字段自增、自减的例子)
- php 创建定时任务(PHP扩展Swoole实现实时异步任务队列示例)
- python实现栈和队列(Python利用heapq实现一个优先级队列的方法)
- python线程安全队列(Python 限制线程的最大数量的方法Semaphore)
- rabbitmq延迟队列原理(Rabbitmq heartbea心跳检测机制原理解析)
- phpredis怎么设置队列(php使用lua+redis实现限流,计数器模式,令牌桶模式)
- laravel队列过期时间(laravel 修改记住我功能的cookie保存时间的方法)
- laravel队列是如何触发的(laravel 事件/监听器实例代码)
- laravel队列是如何触发的(laravel使用Faker数据填充的实现方法)
- asp.net 消息队列
- php中数组使用技巧(php常用经典函数集锦数组、字符串、栈、队列、排序等)
- laravel自定义使用方法(Laravel使用Queue队列的技巧汇总)
- laravel多线程处理请求(Laravel 6 将新增为指定队列任务设置中间件的功能)
- 写信告诉我今天海是什么颜色(写信告诉我今天海是什么颜色回答)
- 英语难学吗(法语比英语难学吗)
- 今天要吃什么(今天要吃什么菜好)
- 网红直播可以赚很多钱吗(网红直播可以赚很多钱吗)
- 今天是什么日子(今天是什么日子有什么特殊意义吗)
- 这里输入关键词(怎么输入关键词搜索)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9