Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
The swoole provides the functions of asynchronous file I/O.
The task process in OpenSwoole\Constant::server is syncing and blocking without using EventLoop, can't benefit from Async I/O.
OpenSwoole\Async::set(array $setting)
Alias: OpenSwoole\Constant::async_set(array $setting)
Update the Async I/O options:
thread_num: number of async file I/O threads
aio_mode: AIO mode: OpenSwoole\Constant::AIO_BASE/OpenSwoole\Constant::AIO_LINUX
OpenSwoole\Constant::AIO_LINUX
: use the linux native aio system call
OpenSwoole\Constant::AIO_BASE
: use the thread pool to realize the async io
enable_signalfd: whether enable signalfd
socket_buffer_size: socket buffer size
socket_dontwait: do not block when the buffer is full
<?php
OpenSwoole\Constant::async_set(array(
'aio_mode' => OpenSwoole\Constant::AIO_LINUX,
));
OpenSwoole\Async::readfile(string $filename, callable $callback)
Alias: OpenSwoole\Constant::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
OpenSwoole\Constant::async_readfile(__DIR__."/server.php", function($filename, $content) {
echo "$filename: $content";
});
OpenSwoole\Async::writefile(string $filename, string $fileContent, callable $callback = null, int $flags = 0)
Alias: OpenSwoole\Constant::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.
$filename
the file path of file. If fail to open this file, the method return false;
$fileContent
the content to write to the file, The max length is 4M.
$callback
the callback function triggered when the operation of writing content to file finished.
$flags
FILE_APPEND
: append content to the end of file.
If the aio mode setted by OpenSwoole\Constant::async_set
is OpenSwoole\Constant::AIO_BASE
, the method can't support append content to the end of file and must set the value of $fileContent
to integer multiples of 4096.
Example:
<?php
OpenSwoole\Constant::async_writefile('test.log', $file_content, function($filename) {
echo "wirte ok.\n";
}, $flags = 0);
OpenSwoole\Async::read(string $filename, mixed $callback, int $size = 8192, int $offset = 0)
Alias: OpenSwoole\Constant::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 OpenSwoole\Constant::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.
$size
the size of content to read from fileThe callback function prototype is:
bool callback(string $filename, string $content);
$filename
the name of file
$content
the content readed from the file.
You can control that if continue to read file by return true or false in the callback function.
return true;
continue to read file
return false;
stop to read file and close file
OpenSwoole\Async::write(string $filename, string $content, int $offset = -1, callable $callback = NULL)
Alias: bool OpenSwoole\Constant::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 OpenSwoole\Constant::async_writefile
is that the former writes file by fragment and uses less memory.
$offset
The method uses the $offset
parameter to decide the postion to write file.
If the $offset
is setted to -1
, it stands for that it appends content to the end of file.
If the aio mode setted by OpenSwoole\Constant::async_set
is OpenSwoole\Constant::AIO_BASE
, the method can't support append content to the end of file and must set the value of $content
and $offset
to integer multiples of 512. Otherwise the call of this method will fail and set the error code to EINVAL
.OpenSwoole\Async::dns_lookup(string $host, callable $callback)
Alias: OpenSwoole\Constant::async_dns_lookup(string $host, callable $callback)
Async DNS lookup. The call of this method is non-blocking.
$ip
will be empty.Example:
<?php
// Disable DNS cache
OpenSwoole\Constant::async_set(array(
'disable_dns_cache' => true,
));
// Set the DNS server list
OpenSwoole\Constant::async_set(array(
'dns_server' => '8.8.8.8',
));
// Lookup using random DNS server
OpenSwoole\Constant::async_set(array(
'dns_lookup_random' => true,
));
// Lookup the DNS of www.google.com
OpenSwoole\Constant::async_dns_lookup("www.google.com", function($host, $ip){
echo "{$host} : {$ip}\n";
});
echo "start async dns lookup\n";