How to implement supervisor to execute asynchronous process in Laravel framework

怪我咯
Release: 2023-03-10 13:54:01
Original
1615 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 (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
Copy after login

Restart supervisor after installation: 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
Copy after login

The process instructions managed by the supervisor are configured here to start monitoring 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

Asynchronous program Write in the app/job directory, create a new class of your own, for example: Shutdown, and 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 detailed content of How to implement supervisor to execute asynchronous process in Laravel framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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