Swoole Coroutine System API

Swoole\Coroutine\System

Since version 4.4.6

Swoole\Coroutine\System::sleep(float $seconds): void

Example:

<?php
go(function() {
    Swoole\Coroutine\System::sleep(0.2);
});
Swoole\Coroutine\System::exec(string $cmd): array

The return value:

<?php
[
    'code' => 0,
    'signal' => 0,
    'output' => '',
]

Example:

<?php
go(function() {
    $ret = Swoole\Coroutine\System::exec("md5sum ".__FILE__);
    print_r($ret);
});
Swoole\Coroutine\System::gethostbyname(string $host, int $family = AF_INET, float $timeout): string | bool

Find one IP address by host name.

$family is AF_INET for IPv4 or AF_INET6 for IPv6.

Example:

<?php
go(function () {
    $ip = Swoole\Coroutine\System::gethostbyname("www.swoole.co.uk", AF_INET, 0.5);
    print_r($ip);
});
Swoole\Coroutine\System::getaddrinfo(string $host, int $family = AF_INET, int $socktype = SOCK_STREAM, int $protocol = IPPROTO_TCP, string $service = null): array | bool

Find multiple IP addresses by host name.

$family is AF_INET for IPv4 or AF_INET6 for IPv6.

Swoole\Coroutine\System::dnsLookup(string $domain, double $timeout = 5): bool

DNS lookup using the DNS in /etc/resolve.conf.

Only IPv4 is supported.

The error details can be found using swoole_last_error().

Swoole\Coroutine\System::fread(resource $handle, int $length = 0): string | false

Read data from file with coroutine support.

Example:

<?php
$fp = fopen(__DIR__ . "/data.txt", "r");
go(function () use ($fp) {
    $r = Swoole\Coroutine\System::fread($fp);
    var_dump($r);
});
Swoole\Coroutine\System::fwrite(resource $handle, string $data, int $length = 0): int | false

Write data into file with coroutine support.

Example:

<?php
$fp = fopen(__DIR__ . "/data.txt", "a+");
go(function () use ($fp) {
    $r = Swoole\Coroutine\System::fwrite($fp, "hello world\n", 5);
    var_dump($r);
});
Swoole\Coroutine\System::readFile(string $filename): string | bool

Read the whole file into memory.

<?php
$filename = __DIR__ . "/data.txt";
go(function () use ($filename) {
    $r = Swoole\Coroutine\System::readFile($filename);
    var_dump($r);
});
Swoole\Coroutine\System::writeFile(string $filename, string $fileContent, int $flags): string | bool

Maximum size of $fileContent is 4MB.

$flags is FILE_APPEND if appending to the file.

Example:

<?php
use Swoole\Coroutine\System;
go(function () {
    $ret =  System::writeFile(__DIR__ ."/data.txt", "Hello World!");
    print_r($ret);
});
Swoole\Coroutine\System::fgets(resource $handle): string | false

Since version 4.4.4

Read data from file with coroutine support.

Example:

<?php
$fp = fopen(__DIR__ . "/data.txt", "r");
go(function () use ($fp) {
    $r = Swoole\Coroutine\System::fgets($fp);
    var_dump($r);
});
Swoole\Coroutine\System::fwrite(resource $handle, string $data, int $length = 0): int | false

Write data into file with coroutine support.

Example:

<?php
$fp = fopen(__DIR__ . "/data.txt", "a+");
go(function () use ($fp) {
    $r = Swoole\Coroutine\System::fwrite($fp, "hello world\n", 5);
    var_dump($r);
});
Swoole\Coroutine\System::statvfs(string $path): array | false

Since version 4.2.5

Get the fs information.

Example:

<?php
go(function() {
    var_dump(Swoole\Coroutine\System::statvfs('/'));
});

// array(11) {
//   ["bsize"]=>
//   int(4096)
//   ["frsize"]=>
//   int(4096)
//   ["blocks"]=>
//   int(61068098)
//   ["bfree"]=>
//   int(45753580)
//   ["bavail"]=>
//   int(42645728)
//   ["files"]=>
//   int(15523840)
//   ["ffree"]=>
//   int(14909927)
//   ["favail"]=>
//   int(14909927)
//   ["fsid"]=>
//   int(1002377915335522995)
//   ["flag"]=>
//   int(4096)
//   ["namemax"]=>
//   int(255)
// }
Swoole\Coroutine\System::waitEvent(mixed $resource, int $events = SWOOLE_EVENT_READ, float $timeout = -1): int | false

Since version 4.5.0

Wait for the I\O event witin Coroutine context.

$resource can be sockets, php_stream, Swoole\Client, Swoole\Process, Swoole\Coroutine\Client, fd.

$events can be SWOOLE_EVENT_READ, SWOOLE_EVENT_WRITE, or SWOOLE_EVENT_WRITE | SWOOLE_EVENT_READ

Example:

<?php
Co\run(function () {
    $stdin = fopen("php://stdin", 'r');
    $ip = Swoole\Coroutine\System::waitEvent($stdin, SWOOLE_EVENT_READ, 5);
    if ($ip) {
      echo fgets($stdin);
    } else {
      echo "timeout\n";
    }
});
Swoole\Coroutine\System::wait(float $timeout = -1): array | false

Since version 4.5.0

Wait event from child process within Coroutine.

Example:

<?php
(new Swoole\Process(function () {
    echo "child process start\n";
    sleep(1);
    echo "child process exit\n";
}))->start();

Co\run(function () {
    $info = Swoole\Coroutine\System::wait(5);
    var_dump($info);
});
Swoole\Coroutine\System::waitPid(int $pid): array | false

Since version 4.5.0

Waiting for event from the process based on PID within Coroutine.

Example:

<?php
$process = new Swoole\Process(function () {
    echo "child process start\n";
    sleep(10);
    echo "child process exit\n";
});

$process->start();

Co\run(function () {
    $info = Swoole\Coroutine\System::waitPid($process->pid, 1);
    var_dump($info);
});
Swoole\Coroutine\System::waitSignal(int $signo, float $timeout = -1): bool

Since version 4.5.0

Waiting for system signal within Coroutine.

Example:

<?php

$pid = getmypid();

$process = new Swoole\Process(function () use ($pid) {
    echo "child process start\n";
    sleep(1);
    Process::kill($pid, SIGUSR1);
    echo "child process exit\n";
});
$process->start();

Co\run(function () {
    $info = Swoole\Coroutine\System::waitSignal(SIGUSR1, 3);
    var_dump($info);
});