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

Swoole AsyncIO instance


May 14, 2021 Swoole


Table of contents


Introduction to the reading and writing of Swoole AsyncIO asynchronous files

Swoole1.6.12 adds asynchronous file read and write, asynchronous DNS and other features. A complete asynchronous parallel API has since been established.

  • swoole_server task Task进程 blocked and EventLoop so you cannot make any asynchronous IO other than the timer
  • Signalfd is a Linux 2.6.27 file handle-based signal processing feature that provides the advantage of adding a signal to EventLoop without being interrupted by a signal to improve performance. The disadvantage is that some synchronously blocked programs may have problems that cannot be interrupted from blocking, 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.

swoole_async_set

This function can set asynchronous IO-related options.

swoole_async_set(array $setting);
  • thread_num set the number of Asynchronous file IO threads
  • aio_mode to set the operating mode of the asynchronous file IO, which currently SWOOLE_AIO_BASE (using thread pool synchronization blocking simulation asynchronous similar to SWOOLE_AIO_LINUX Native AIO) modes
  • enable_signalfd on and off the use of the signalfd feature
  • socket_buffer_size set the SOCKET memory cache size
  • socket_dontwait prevents underlying blocking from waiting when the memory cache is full

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 asynchronous IO

Linux Native AIO system calls are true asynchronous IOs, not blocking simulations.

Advantages:

  • All operations are done in one thread and no thread pool is required
  • Do not rely on threads to perform IO, so the co-operation can be very large

Disadvantages:

  • Only DriveIO is supported, PageCache is not available, and all read and write to the file operates directly on the disk

Thread pool mode asynchronous IO

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:

  • The operating system PageCache can be used to read and write hot data performance is very high, equal to reading memory

You can modify thread_num of AIO threads enabled by the two item settings

Disadvantages:

  • The number of threads with AIO is limited by poor co-operation, which does not support reading and writing a large number of files at the same time

A simple example:

$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";
});