May 14, 2021 Swoole
This channel details the differences between asynchronous programming and synchronous programming and what needs to be noted.
No one is very prone to this error, because the swoole is resident in memory, so the file defined by the class/function is loaded and not released. Therefore, the php file of a class/function must be introduced using include_once or require_once, whether a fatal error of the can redeclare function/class will occur.
PhP daemons are completely different from ordinary Web programs in their variable lifecycle and memory management. R efer to swoole_server memory management page. Special swoole_server be taken when writing your data or other resident processes.
Process isolation is also a common problem for many nods. T he value of the global variable is modified, why does not take effect, because the global variable is in different processes, the memory space is isolated, so it is not valid. So using swoole to develop a server program requires an understanding of process isolation issues.
In asynchronous IO programs, sleep/usleep/time_sleep_until/time_nanosleep. (Sleep is used below to refer to all sleep functions in general)
Swoole provides swoole_event_add, swoole_timer_tick, swoole_timer_after, swoole_process::signal, asynchronous swoole_client will stop working after the process sleep. swoole_server can no longer process new requests.
$serv = new swoole_server("127.0.0.1", 9501); $serv->on('receive', function ($serv, $fd, $from_id, $data) { sleep(100); $serv->send($fd, 'Swoole: '.$data); }); $serv->start();
The sleep function was executed in the onReceive event, and the server could no longer receive any client requests in 100 seconds.
The use of exit/die is prohibited in the swoole program, and if there is an exit/die in the PHP code, the worker process, Task process, User process, and swoole_process currently working exit immediately.
It is recommended to replace the exit/die with try/catch, implementing interrupt execution to jump out of the PHP function call stack.
function swoole_exit($msg) { //php-fpm的环境 if (ENV=='php') { exit($msg); } //swoole的环境 else { throw new Swoole\ExitException($msg); } }
Exceptions are handled more friendlyly than exit/die because exceptions are controllable and exit/die is not controllable. T ry/catch at the outerst layer to catch exceptions and terminate only the current task. T he Worker process can continue to process new requests, and exit/die causes the process to exit directly and all variables and resources saved by the current process to be destroyed. If there are other tasks to process within the process, encountering exit/die will also be discarded all.
If an asynchronous program encounters a dead loop, the event cannot be triggered. T he asynchronous IO program uses the Reactor model and must be polled at reactor-gt;wait during operation. If a dead loop is encountered, the program's control is in while, the reactor cannot take control, the event cannot be detected, and the IO event callback function will not fire.
The code for intensive operations is not blocked
$serv = new swoole_server("127.0.0.1", 9501); $serv->on('receive', function ($serv, $fd, $from_id, $data) { while(1) { $i ++; } $serv->send($fd, 'Swoole: '.$data); }); $serv->start();
A dead loop was executed in the onReceive event, and the server can no longer receive any client requests and must wait for the loop to end before continuing to process the new event.