Swoole learning summary 1

1. There are three modes of Swoole service (the default is multi-process mode)

# Base mode (SWOOLE_BASE ) 
Traditional asynchronous non-blocking Server , reactor and worker are the same role. TCP connections are maintained within the worker process.
If no interaction is required between client connections, BASE mode can be used. Such as Memcache , Http server, etc.

#Thread mode
Multi-threaded Worker mode, Reactor thread to process network event polling and read data. The obtained request is handed over to the Worker thread for processing.

Disadvantage: A memory error occurs in one thread, and the entire process ends.
Due to a memory bug in PHP 's ZendVM in multi-threaded mode, multi-threaded mode has been turned off after v1.6.0.

#Process mode is different
from multi-threaded Worker mode in that threads are replaced by processes. Reactor threads to handle network event polling and read data. The obtained request is handed over to the Worker process for processing. It is suitable for scenarios with very complex business logic. Such as WebSocket server, etc.

Second, the relationship between processes:

a most basic Swoole Server needs at least 3 processes, namely the Master process, Manager process and Worker process



The Master process is a multi-threaded process, which has a very important set of threads called Reactor threads (groups). Whenever a client connects to the server, the Master process will select from the existing Reactor threads according to certain rules. One 
is responsible for providing the client with services such as maintaining links, processing network IO, and sending and receiving data. Functions such as unpacking and unpacking are also completed here.


The Manager process can be regarded as an agent layer in a sense. It does not directly handle business itself. Its main job is to transfer the data received in the Master process to the Worker process, or to send the data in the Worker process to the client. Handed over to the Master process for sending.

The Manager process is also responsible for monitoring the Worker process. If the Worker process hangs up unexpectedly, the Manager process will restart the new Worker process, which is a bit like the work of the Supervisor. And this feature is also the core mechanism for the final realization of hot reloading.

The Worker process is actually a process that handles various business tasks. The Manager transfers the data packets to the Worker process, and then the Worker process performs specific processing and feeds back the results to the client according to the actual situation.


Three, programming instructions
Programming Notes 

1. Do not execute sleep and other sleep functions in the code, which will cause the entire process to block
2. Exit/die is dangerous and will cause the worker process to exit
3. You can use register_shutdown_function to catch fatal errors when the process exits abnormally Do some request work, see /wiki/page/305.html for details 4. If an exception is thrown in the PHP code, you must do a try/catch
in the callback function to catch the exception, otherwise it will cause the worker process to exit 5. swoole does not support set_exception_handler , you must use the try/catch method to handle exceptions . 6. Worker processes must not share the same network service client such as Redis or MySQL. The relevant code for Redis/MySQL connection creation can be placed in the onWorkerStart callback function. The reason is that if one connection is shared , the returned result cannot be guaranteed by which process. The process holding the connection can theoretically read and write to this connection, so the data will be confused. For details, please refer to /wiki/page/325.html 7. You cannot use class attributes to save client connection information, because a worker process can handle multiple client connections, resulting in disordered class attribute data. Constants are fine.



 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325115449&siteId=291194637