Table of Contents
1.1 Command mode
2.2 Command line parameters
Home PHP Framework ThinkPHP Will you use thinkphp-queue in ThinkPHP5?

Will you use thinkphp-queue in ThinkPHP5?

Jul 17, 2020 pm 01:23 PM
thinkphp5

The following tutorial column of thinkphp framework will introduce to you the method of using thinkphp-queue in ThinkPHP5. I hope it will be helpful to friends in need!

Will you use thinkphp-queue in ThinkPHP5?

Recently, the editor received a message from friends in the group who didn’t know how to use php-queue when using the ThinkPHP5.0 framework (it’s really embarrassing, there are many comments about this on the Internet) tutorial). Haha, it doesn’t matter if you don’t want to refer to it. Let me go through it in detail~~ About what is a queue? What does the queue solve? What are the scenarios for queues? ...and so on. Developers should first understand these issues by themselves. In addition, this case only performs specific operations on the ThinkPHP framework.

1. Install thinkphp-queue

It is recommended to use Composer for installation, switch to the root directory of the project, and execute: composer require topthink/think-queue

2. Configure the storage environment of the message queue

The configuration file is located in application\extra\queue.php, as shown in the figure: Will you use thinkphp-queue in ThinkPHP5?

Use the Redis driver. If the Redis environment has not been installed, Please install it yourself, it is very simple, or you can use other types of drivers, as shown in the figure: Will you use thinkphp-queue in ThinkPHP5?

3. Task creation

We have the Index.php file under the index module , create a method named queue (does it have to be like this? Of course not, this tutorial is just an example, you can create a new module, and the method name can be other), as shown in the figure:

Will you use thinkphp-queue in ThinkPHP5?

Will you use thinkphp-queue in ThinkPHP5?

4. Task message consumption and deletion processing

We create a job file under the index module, which is used to process the consumption mode, as shown in the figure:Will you use thinkphp-queue in ThinkPHP5?

Under the Demo class, create a fire method, which is the method called by the message queue by default. As shown in the picture:

Will you use thinkphp-queue in ThinkPHP5?

# The above is the creation of the completed task--"Push--"Consumption.
In the browser or terminal, execute our index/queue.html method to complete the creation and push. Regarding task consumption, you can refer to the command and switch to the root directory of the project in the terminal:

5. Monitor tasks and execute

php think queue:listen
php think queue:work –daemon(不加–daemon为执行单个任务)
Copy after login

. For specific optional parameters, you can enter the command and add –help to view.

can be used with supervisor to ensure that the process is resident

1.1 Command mode

  • queue:subscribe command [As of 2017-02-15, the author has not yet Implement this mode, skip]

  • queue:work command

    work command: This command will start a work process to process the message queue.

    php think queue:work --queue helloJobQueue
    Copy after login
  • queue:listen command

    listen command: This command will create a listen parent process, and then the parent process will pass proc_open('php think queue :work') to create a work sub-process to process the message queue and limit the execution time of the work process.

    php think queue:listen --queue helloJobQueue
    Copy after login

2.2 Command line parameters

  • Work mode

    php think queue:work \
    --daemon            //是否循环执行,如果不加该参数,则该命令处理完下一个消息就退出
    --queue  helloJobQueue  //要处理的队列的名称
    --delay  0 \        //如果本次任务执行抛出异常且任务未被删除时,设置其下次执行前延迟多少秒,默认为0
    --force  \          //系统处于维护状态时是否仍然处理任务,并未找到相关说明
    --memory 128 \      //该进程允许使用的内存上限,以 M 为单位
    --sleep  3 \        //如果队列中无任务,则sleep多少秒后重新检查(work+daemon模式)或者退出(listen或非daemon模式)
    --tries  2          //如果任务已经超过尝试次数上限,则触发‘任务尝试次数超限’事件,默认为0
    Copy after login
  • Listen mode

    php think queue:listen \
    --queue  helloJobQueue \   //监听的队列的名称
    --delay  0 \         //如果本次任务执行抛出异常且任务未被删除时,设置其下次执行前延迟多少秒,默认为0
    --memory 128 \       //该进程允许使用的内存上限,以 M 为单位
    --sleep  3 \         //如果队列中无任务,则多长时间后重新检查,daemon模式下有效
    --tries  0 \         //如果任务已经超过重发次数上限,则进入失败处理逻辑,默认为0
    --timeout 60         //创建的work子进程的允许执行的最长时间,以秒为单位
    Copy after login

    You can see that in listen mode, the --deamon parameter is not included. The reason will be explained below

Linux starts the daemon process, taking tp5 as an example

nohup php think queue:work --daemon --queue 任务名称 &
 nohup php think queue:work --daemon --queue 任务名称 &
Copy after login

To check the number of tasks in Linux, you can enter: jobs -l

Will you use thinkphp-queue in ThinkPHP5?

in the terminal

The above is the detailed content of Will you use thinkphp-queue in ThinkPHP5?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I get an error when deploying thinkphp5 in Pagoda? What should I do if I get an error when deploying thinkphp5 in Pagoda? Dec 19, 2022 am 11:04 AM

Solution to the error reported when deploying thinkphp5 in Pagoda: 1. Open the Pagoda server, install the php pathinfo extension and enable it; 2. Configure the ".access" file with the content "RewriteRule ^(.*)$ index.php?s=/$1 [QSA ,PT,L]”; 3. In website management, just enable thinkphp’s pseudo-static.

What should I do if thinkphp5 url rewriting fails? What should I do if thinkphp5 url rewriting fails? Dec 12, 2022 am 09:31 AM

Solution to thinkphp5 url rewriting not working: 1. Check whether the mod_rewrite.so module is loaded in the httpd.conf configuration file; 2. Change None in AllowOverride None to All; 3. Modify the Apache configuration file .htaccess to "RewriteRule ^ (.*)$ index.php [L,E=PATH_INFO:$1]" and save it.

How to get the requested URL in thinkphp5 How to get the requested URL in thinkphp5 Dec 20, 2022 am 09:48 AM

Methods for thinkphp5 to obtain the requested URL: 1. Use the "$request = Request::instance();" method of the "\think\Request" class to obtain the current URL information; 2. Use the built-in helper function "$request-> url()" to obtain the complete URL address including the domain name.

What should I do if thinkphp5 post cannot get the value? What should I do if thinkphp5 post cannot get the value? Dec 06, 2022 am 09:29 AM

thinkphp5 post cannot get a value because TP5 uses the strpos function to find the app/json string in the content-type value of the Header. The solution is to set the content-type value of the Header to app/json.

How to remove thinkphp5 title bar icon How to remove thinkphp5 title bar icon Dec 20, 2022 am 09:24 AM

How to remove the thinkphp5 title bar icon: 1. Find the favicon.ico file under the thinkphp5 framework public; 2. Delete the file or choose another picture to rename it to favicon.ico and replace the original favicon.ico file.

What should I do if thinkphp5 prompts that the controller does not exist? What should I do if thinkphp5 prompts that the controller does not exist? Dec 06, 2022 am 10:43 AM

Solution to thinkphp5 prompting that the controller does not exist: 1. Check whether the namespace in the corresponding controller is written correctly and change it to the correct namespace; 2. Open the corresponding tp file and modify the class name.

How to query yesterday's data in ThinkPHP5 How to query yesterday's data in ThinkPHP5 Dec 05, 2022 am 09:20 AM

How to query yesterday's data in ThinkPHP5: 1. Open ThinkPHP5 related files; 2. Query yesterday's data through the expression "db('table')->whereTime('c_time', 'yesterday')->select();" Can.

How to set error prompts in thinkphp5 How to set error prompts in thinkphp5 Dec 07, 2022 am 10:31 AM

How to set error prompts in thinkphp5: 1. Enter the public folder in the project root directory and open the index.php entry file; 2. View the comments on the debug mode switch; 3. Adjust the value of the "APP_DEBUG" constant to true to display Error message prompt.

See all articles