This article mainly introduces how to implement the supervisor to execute asynchronous processes in the PHP framework Laravel. This article is very detailed and I believe it has some reference and learning value for everyone, let's take a look. This article mainly introduces how to implement the supervisor to execute asynchronous processes in the PHP framework Laravel. This article is very detailed and I believe it has some reference and learning value for everyone, let's take a look.
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.
The above is a detailed description of how the supervisor executes asynchronous processes in the Laravel framework. For more information, see other related articles in the first PHP community!