PHP framework Laravel implements the supervisor to execute asynchronous processes, laravelsupervisor

Source: Internet
Author: User

PHP framework Laravel implements the supervisor to execute asynchronous processes, laravelsupervisor

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.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.