Join 4,000+ others and never miss out on new tips, tutorials, and more.
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
false, allows you to decide between restarting the whole server or just task worker threads
If success, it returns a
true otherwise it returns
OpenSwoole loads all PHP files into memory and reuses the code for serving different requests. New changes to any code can't be seen immediately like with PHP-FPM because FPM uses a stateless shared nothing design, OpenSwoole is constant, using a stateful design, the script is started from PHP CLI and runs in memory. You have to reload the changed files with the hot reload function on demand.
inotify can be used in Linux to monitor the changed files, then trigger the reload function. But please make sure
inotify is supported by your server OS. Or you can use
reload() function will safely restart all the worker processes gracefully and reload all PHP files without interrupting the running server.
Files included within
onReceive can be reloaded without restarting the server, but you can't reload files included before the start of the
You can't reload the runtime setting of the OpenSwoole Server, you must stop and start the server again.
You can reload files included in Task Workers only with
$onlyReloadTaskworker = true.
The whole point of hot code reloading is to aid in development, making it easier to test new code changes locally or over a network but the reload functionality can be used in production. OpenSwoole will not accept multiple reload events if one is currently in place, if new reload signals get sent, they will be discarded until the current active reload has completed. - A complete reload means OpenSwoole has to wait for worker threads to finish handling a request and the same goes for task workers, everything is handled in a graceful manner.
Trigger a worker or task worker reload by using the reload function:
$server->reload(). You can work out if you need to restart or not using PHP.
You can also trigger the hot code reloading function with Linux signals:
Restart both workers and task workers:
kill -USR1 MASTER_PID
Only restart the task processes by signal:
kill -USR2 MASTER_PID
You can see which files are loaded before the worker started:
$server->on('WorkerStart', function($serv, $workerId)
// Files which won't be reloaded
// Include files from here so they can be reloaded...
The files which will be dumped from the above example cannot be reloaded because they have been included before the worker starts, files need to be included during a worker start event
stat in APC/OpCache has to be enabled for code reloading
You can handle any pre or post processing tasks before a reload event and check included files.
$server->on('BeforeReload', function($serv, $workerId)
Since version 4.5.0
Callback function before the code reloading event takes place.
$server->on('AfterReload', function($serv, $workerId)
Since version 4.5.0
Callback function when after the code reloading event takes place.
A OpenSwoole server can be started using two different modes, we have
OpenSwoole\Server::SIMPLE_MODE, consider each mode when reloading files.
In process mode, multiple threads are started which manage different areas of the OpenSwoole server, we have a Master (main) process, a manager process which handles all the worker tasks. During a reload only the worker threads are restarted but only when they are not busy.
In base mode, because connections interact with the worker thread directly, if a reload happens the connect is cut off and thus, a graceful restart won't happen in this server mode. Also base mode does not support reloading task workers.
Please visit the common questions page for more comments on hot reloading.
You could use
addListener to add another port for the server to use and listen for server reload events so that you can externally reload the server and then use