How to implement supervisor to execute asynchronous process in Laravel framework in PHP

不言
Release: 2023-03-31 22:32:02
Original
1435 people have browsed it

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.

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 is a process management tool written in Python, which can be used very conveniently. 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
Copy after login

Restart after installation Supervisor:

service supervisor restart
Copy after login

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
Copy after login

The process instructions managed by the supervisor are configured here to start monitoring of the 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,
 ],
Copy after login

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));
Copy after login

Use this instruction to create an asynchronous program.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of event operation in PHP's Laravel framework

About php artisan under laravel 5.1 Use of migrate

The above is the detailed content of How to implement supervisor to execute asynchronous process in Laravel framework in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!