Home  >  Article  >  Backend Development  >  Detailed explanation of Laravel's method of implementing supervisor to execute asynchronous processes

Detailed explanation of Laravel's method of implementing supervisor to execute asynchronous processes

*文
*文Original
2018-01-03 13:54:522013browse

This article mainly introduces you to the method of implementing supervisor to execute asynchronous processes in the PHP framework Laravel. The introduction in the article is very detailed. I believe it has certain reference and learning value for everyone. Friends who need it can take a look below. I hope to be helpful.

Problem description

When you use the Laravel framework to implement dynamic web pages, if some operations require a large amount of calculation, in order not to affect the user experience, It is often necessary to use asynchronous processing. This is implemented using the queues that come with supervisor and laravel. Let’s take a look at the detailed introduction:

Supervisor

Supervisor (http://supervisord.org) is a process written in Python Management tools can be easily used to start, restart, and shut down processes (not just Python processes). In addition to controlling a single process, you can also start and shut down multiple processes at the same time. For example, unfortunately, a server problem causes all applications to be killed. At this time, you can use supervisor to start all applications at the same time instead of typing commands one by one. start up.

The method is as follows

1. Install supervisor

apt-get install supervisor

Restart after installation supervisor: service supervisor restart

2. Configure supervisor file

[program:laravel]
process_name=%(program_name)s_%(process_num)02d
command=php {{ app.root }}/artisan queue:listen
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile={{ app.root }}/storage/logs/queque.log

The process instructions managed by supervisor are configured here for startup Monitoring of laravel queue. The number of processes and the error log address are also specified.

3. Queue configuration

Before writing the queue code, you need to configure the queue. Its configuration location is in config/queue.PHP. Here you need to configure Your queue drivers such as database, Redis, sync, etc. Different options have corresponding configurations. I choose redis for configuration here.

For example:

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

4. Write an asynchronous program

Write the asynchronous program in the app/job directory and create a new one of your own Class, for example: Shutdown, write the constructor and handle function of the class. The handle function is the program calculation process.

5. Execute asynchronous function

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

Use this instruction to create an asynchronous program.

Related recommendations:

Summary of the problem of php artisan migrate in laravel

Detailed explanation of Laravel's task scheduling console

Detailed explanation of how Laravel modifies .env configuration through the background

The above is the detailed content of Detailed explanation of Laravel's method of implementing supervisor to execute asynchronous processes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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