Here is a demonstration of the ZPHP framework, which implements swoole’s code hot update. In the WorkerStart callback, load the ZPHP framework:

use ZPHPZPHP; $zphp = null; $mimes = null; $HTTP = new swoole_http_server (' 0.0.0.0, 9501); $http->on('request', function (swoole_http_request $request, swoole_http_response $response){ //...... }); $http->on('WorkerStart',function($serv, $worker_id){// Frame load require __dir__.directory_separator. 'ZPHP'. DIRECTORY_SEPARATOR . 'ZPHP.php'; global $zphp; $zphp = ZPHP::run(__DIR__, false, 'default'); global $mimes; $mimes = require "mimes.php"; }); $http->start();Copy the code

The file name is http_server.php

Run this script in the background:

php http_server.php &
Copy the code

Enter 192.168.1.116:9501 in the browser to make an HTTP request:

This is because after loading the ZPHP framework, the default methods under the default controller are accessed, with a line of code like:

$data = $project."zchat runing in swoole!!!! n";Copy the code

Now modify this line as follows:

$data = $project."The code is modified!!!! n";Copy the code

View the http_server process in Linux

ps axuf|grep http_server
Copy the code

As shown in the figure, from top to bottom are swoole’s master process, Manager process and four worker processes respectively. The worker process is created by the Manager process. To hot-update the code, you need to reload the worker process

Send a signal to the manager process to reload the worker process with the following command:

kill -USR1 5913
Copy the code

Look again at the http_server process



It can be seen that the process numbers of the four worker processes are different from before, which indicates that the Manager process has overloaded the worker process

Refresh page visibility in browser

Hot update succeeded ~

Here’s a quick summary:

The code hot update actually updates the contents of the “WorkerStart” callback, which means that all of our business code goes into the “WorkerStart” callback.

In addition, the command ‘kill -usr1 process number ‘is explained as follows:

“USR1 is also commonly used to tell applications to reload configuration files; For example, sending a USR1 signal to the Apache HTTP server will cause the following steps to occur: stop accepting new connections, wait for the current connection to stop, reload the configuration file, reopen the log file, and restart the server for a relatively smooth no-shutdown change.

The original:Blog.csdn.net/MaleicAcid/…