This article mainly introduces the PHP framework Laravel implementation of Supervisor implementation of asynchronous process, the article introduces very detailed, I believe that we have a certain reference learning value, the need for friends below to see it together.
Problem description
When you use the Laravel framework to implement dynamic Web pages, if some operations are computationally large, in order to not affect the user experience, it is often necessary to use the asynchronous way to deal with. This is achieved using supervisor and Laravel's own queues. Let's take a look at the detailed introduction below:
Supervisor
Supervisor (http://supervisord.org) is a python-written process management tool that can be easily used to start, restart, and close processes (not just the Python process). In addition to controlling a single process, you can start and close multiple processes at the same time, such as unfortunate server problems that cause all applications to be killed, and you can start all applications with supervisor at the same time instead of one by one.
Methods are as follows
1. Installing Supervisor
Apt-get Install Supervisor
Restart Supervisor after installation:service supervisor restart
2. Configuring 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 supervisor managed process instructions are configured to initiate the Laravel queue monitoring. Both the number of processes and the error log address are specified.
3. Queue Configuration
Before you write the queue code, you need to configure the queues, where they are configured in Config/queue. In PHP, here you need to configure your queue drives, such as databases, Redis, synchronization, and so on. Different options are configured accordingly. I choose Redis for configuration here.
For example:
' Redis ' = [ ' driver ' = ' redis ', ' connection ' + env (' queue_connection ', ' Default '), ' QUEUE ' = > ' wordtohtml ', ' expire ' = 600,],
4. Writing Asynchronous programs
The asynchronous program is written under the App/job directory, creating a new class of its own, such as: Shutdown, writing constructors and handle functions for the class. The handle function is the process of program calculation.
5. Executing asynchronous functions
$this->dispatch (New Shutdown ($VIMD));
Using this command, you can create an asynchronous program.
Summarize