May 14, 2021 Swoole
Swoole1.6.12 adds asynchronous file read and write, asynchronous DNS and other features. A complete asynchronous parallel API has since been established.
Task进程
blocked and
EventLoop
so you cannot make any asynchronous IO other than the timer
swoole_async_set
turn off the
signalfd
feature
Swoole supports 3 types of asynchronous file read and write IO,
swoole_async_set
set AIO mode.
This function can set asynchronous IO-related options.
swoole_async_set(array $setting);
SWOOLE_AIO_BASE
(using thread pool synchronization blocking simulation asynchronous similar to
SWOOLE_AIO_LINUX
Native AIO) modes
The advantage of Linux Native AIO is that the kernel support is the real asynchronous file IO, the disadvantage is that it only supports DirectIO and cannot take advantage of PageCache to the system
swoole_async module is currently experimental in nature and is not recommended for use in production environments, please use PHP's file read and write function.
Linux Native AIO system calls are true asynchronous IOs, not blocking simulations.
Advantages:
Disadvantages:
Based on the thread pool simulation implementation, file read and write requests are posted to the task queue, and then the AIO thread reads and writes the file, notifying the main thread when it is complete. T he AIO thread itself is blocked synchronously. So it's not really asynchronous IO.
Advantages:
You can modify
thread_num
of AIO threads enabled by the two item settings
Disadvantages:
$fp = stream_socket_client("tcp://127.0.0.1:80", $code, $msg, 3);
$http_request = "GET /index.html HTTP/1.1\r\n\r\n";
fwrite($fp, $http_request);
Swoole\Event::add($fp, function($fp){
echo fread($fp, 8192);
swoole_event_del($fp);
fclose($fp);
});
Swoole\Timer::after(2000, function() {
echo "2000ms timeout\n";
});
Swoole\Timer::tick(1000, function() {
echo "1000ms interval\n";
});