Swoole Async File I/O

The swoole provides the functions of asynchronous file I/O.

The task process in swoole_server is syncing and blocking without using EventLoop, can't benefit from Async I/O.

Swoole\Async::set(array $setting)

Alias: swoole_async_set(array $setting)

Update the Async I/O options:

<?php
swoole_async_set(array(
    'aio_mode' => SWOOLE_AIO_LINUX,
));

Swoole\Async::readfile(string $filename, callable $callback)

Alias: swoole_async_readfile(string $filename, callable $callback);

Read files in the async way. When the operation of reading content from file finished, the callback registered is callback automatically.

The max length of file is 4M.

Example:

<?php
swoole_async_readfile(__DIR__."/server.php", function($filename, $content) {
     echo "$filename: $content";
});

Swoole\Async::writefile(string $filename, string $fileContent, callable $callback = null, int $flags = 0)

Alias: swoole_async_writefile(string $filename, string $fileContent, callable $callback = null, int $flags = 0)

Write file in the async way. When the operation of writing content to file finished, the callback registered is callback automatically.

Example:

<?php
swoole_async_writefile('test.log', $file_content, function($filename) {
    echo "wirte ok.\n";
}, $flags = 0);

Swoole\Async::read(string $filename, mixed $callback, int $size = 8192, int $offset = 0)

Alias: swoole_async_read(string $filename, mixed $callback, int $size = 8192, int $offset = 0)

Read the file content stream in the async way. When the operation of reading content from file finished, the callback registered is callback automatically.

The difference between this method and swoole_async_readfile is that the former reads file by fragment and uses less memory. This method reads content of $size length from file every time and can be used to read big file.

The callback function prototype is:

bool callback(string $filename, string $content);

You can control that if continue to read file by return true or false in the callback function.

Swoole\Async::write(string $filename, string $content, int $offset = -1, callable $callback = NULL)

Alias: bool swoole_async_write(string $filename, string $content, int $offset = -1, callable $callback = NULL);

Write file stream in the async way. When the operation of writing content to file finished, the callback registered is callback automatically.

The difference between this method and swoole_async_writefile is that the former writes file by fragment and uses less memory.

Swoole\Async::dns_lookup(string $host, callable $callback)

Alias: swoole_async_dns_lookup(string $host, callable $callback)

Async DNS lookup. The call of this method is non-blocking.

Example:

<?php
// Disable DNS cache
swoole_async_set(array(
    'disable_dns_cache' => true,
));
// Set the DNS server list
swoole_async_set(array(
    'dns_server' => '8.8.8.8',
));
// Lookup using random DNS server
swoole_async_set(array(
    'dns_lookup_random' => true,
));

// Lookup the DNS of www.google.com
swoole_async_dns_lookup("www.google.com", function($host, $ip){
    echo "{$host} : {$ip}\n";
});
echo "start async dns lookup\n";