Implementing asynchronous processes in PHP framework Laravel Supervisor

Source: Internet
Author: User
This article mainly introduces the PHP framework Laravel implementation of Supervisor implementation of asynchronous process, the article introduces very detailed, I believe that we have a certain reference learning value, the need for friends below to see it together.

Problem description

When you use the Laravel framework to implement dynamic Web pages, if some operations are computationally large, in order to not affect the user experience, it is often necessary to use the asynchronous way to deal with. This is achieved using supervisor and Laravel's own queues. Let's take a look at the detailed introduction below:

Supervisor

Supervisor (http://supervisord.org) is a python-written process management tool that can be easily used to start, restart, and close processes (not just the Python process). In addition to controlling a single process, you can start and close multiple processes at the same time, such as unfortunate server problems that cause all applications to be killed, and you can start all applications with supervisor at the same time instead of one by one.

Methods are as follows

1. Installing Supervisor

Apt-get Install Supervisor

Restart Supervisor after installation:service supervisor restart

2. Configuring the Supervisor File

[program:laravel]process_name=% (Program_name) s_% (process_num) 02dcommand=php {{app.root}}/artisan queue: listenautostart=trueautorestart=trueuser=www-datanumprocs=1redirect_stderr=truestdout_logfile={{App.root}}/ Storage/logs/queque.log

The supervisor managed process instructions are configured to initiate the Laravel queue monitoring. Both the number of processes and the error log address are specified.

3. Queue Configuration

Before you write the queue code, you need to configure the queues, where they are configured in Config/queue. In PHP, here you need to configure your queue drives, such as databases, Redis, synchronization, and so on. Different options are configured accordingly. I choose Redis for configuration here.

For example:

' Redis ' = [  ' driver ' = ' redis ',  ' connection ' + env (' queue_connection ', ' Default '),  ' QUEUE ' = > ' wordtohtml ',  ' expire ' = 600,],

4. Writing Asynchronous programs

The asynchronous program is written under the App/job directory, creating a new class of its own, such as: Shutdown, writing constructors and handle functions for the class. The handle function is the process of program calculation.

5. Executing asynchronous functions

$this->dispatch (New Shutdown ($VIMD));

Using this command, you can create an asynchronous program.

Summarize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.