Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What you need to know about Swoole programming


May 14, 2021 Swoole


Table of contents


Programming information

This channel details the differences between asynchronous programming and synchronous programming and what needs to be noted.

Precautions

  • Do not execute sleep and other sleep functions in your code, which can cause the entire process to block
  • Exit/die is dangerous and causes the worker process to exit
  • You register_shutdown_function a fatal error and do some request work when a process exception exits
  • If an exception is thrown in PHP code, the try/catch catch exception must be caught in the callback function, otherwise the work process exits
  • Swoole does not support set_exception_handler and must handle exceptions using try/catch
  • The Worker process must not share a network service client such as Redis or MySQL, and the code associated with the Connection created by Redis/MySQL can be placed in the onWorkerStart callback function.

Classes/functions are repeatedly defined

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.

Memory management

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

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.

  • PhP variables in different processes are not shared, even if the global variable modifies its value within the A process and is not valid within the B process
  • If you need to share data within different Worker processes, you can do so using tools such as Redis, MySQL, Files, Swoole,Table, APCu, shmget, and more
  • The file handles for different processes are isolated, so a Socket connection or open file created by the A process is not valid within the B process, and even sending its fd to the B process is not available

Sleep/usleep effects

In asynchronous IO programs, sleep/usleep/time_sleep_until/time_nanosleep. (Sleep is used below to refer to all sleep functions in general)

  • The sleep function causes the process to get stuck in sleep
  • The operating system does not wake up the current process until the specified time
  • During sleep, only the signal can be interrupted
  • Because the signal processing of swoole is based on signalfd, even sending a signal cannot interrupt the sleep

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.

The instance program

$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 effect of the exit/die function

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.


The effect of the while loop

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

The instance program

$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.