PHP framework Laravel implements the supervisor to execute asynchronous processes, laravelsupervisor
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.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.