Implementation of supervisor asynchronous process in Laravel framework-php Tutorial

Source: Internet
Author: User
This article mainly introduces how to implement the supervisor to execute asynchronous processes in the PHP framework Laravel. This article is very detailed and I believe it has some reference and learning value for everyone, let's take a look. This article mainly introduces how to implement the supervisor to execute asynchronous processes in the PHP framework Laravel. This article is very detailed and I believe it has some reference and learning value for everyone, let's take a look.

Problem Description

When using the Laravel framework to implement dynamic web pages, if some operations require a large amount of computing, asynchronous processing is often required to avoid affecting user experience. Here we use the queues implementation provided by supervisor and laravel. Next let's take a look at the detailed introduction:

Supervisor

Supervisor (http://supervisord.org) is a process management tool written in Python that can be easily used to start, restart, and close processes (not just Python processes ). In addition to controlling a single process, you can also start and close multiple processes at the same time. for example, unfortunately, a server problem causes all applications to be killed, in this case, you can use the supervisor to start all applications at the same time instead of running commands one by one.

The method is as follows:

1. install supervisor

 apt-get install supervisor

Restart supervisor after installation:service supervisor restart

2. configure 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 process commands managed by the supervisor are configured to start the monitoring of the laravel queue. Process count and error log address are also specified.

3. queue configuration

Before writing the queue code, you need to configure the queue in config/queue. PHP. here you need to configure your queue drive, such as database, Redis, and synchronization. Different options have corresponding configurations. Here I select redis for configuration.

For example:

'redis' => [  'driver' => 'redis',  'connection' => env('QUEUE_CONNECTION', 'default'),  'queue' => 'wordtohtml',  'expire' => 600, ],

4. write asynchronous programs

Write asynchronous programs in the app/job directory and create a new class, for example, Shutdown. Compile the class constructor and handle function. The handle function is a program computing process.

5. execute asynchronous functions

$this->dispatch(new Shutdown($vimd));

You can use this command to create an asynchronous program.

The above is a detailed description of how the supervisor executes asynchronous processes in the Laravel framework. For more information, see other related articles in the first PHP community!

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.