Home >Backend Development >PHP Tutorial >Implement supervisor to execute asynchronous process in php framework Laravel

Implement supervisor to execute asynchronous process in php framework Laravel

陈政宽~
陈政宽~Original
2017-06-28 13:42:251567browse

This article mainly introduces to you PHP frameworkThe method of implementing supervisor to execute asynchronous processes in Laravel. The article introduces it in very detail. I believe it has certain reference and learning value for everyone. Friends who need it can follow Let’s take a look together.

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 driver , such as database, Redis, synchronization, 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.

Summarize

The above is the detailed content of Implement supervisor to execute asynchronous process in php framework Laravel. 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