#php #async #macro #php-tokio

php-tokio-derive

为php-tokio提供衍生宏

2个不稳定版本

0.2.0 2023年10月11日
0.1.0 2023年8月27日

#61 in #php

26 monthly downloads
用于 php-tokio

Apache-2.0

7KB
130 代码行

php-tokio - 从PHP使用任何异步Rust库!

由 Daniil Gentili (@danog) 创建。

这个库允许您从PHP异步地使用任何异步Rust库。

它与 revolt 完全集成:这允许与 amphpPSL 和 reactphp 实现完全兼容。

示例

以下是一个示例,使用异步Rust库 reqwest 从PHP发送异步HTTP请求

<?php

use Reqwest\Client;

use function Amp\async;
use function Amp\Future\await;

require 'vendor/autoload.php';

Client::init();

function test(int $delay): void {
    $url = "https://httpbin.org/delay/$delay";
    $t = time();
    echo "Making async reqwest to $url that will return after $delay seconds...".PHP_EOL;
    Client::get($url);
    $t = time() - $t;
    echo "Got response from $url after ~".$t." seconds!".PHP_EOL;
};

$futures = [];
$futures []= async(test(...), 5);
$futures []= async(test(...), 5);
$futures []= async(test(...), 5);

await($futures);

用法

cd examples/reqwest && \
    cargo build && \
    composer update && \
    php -d extension=../../target/debug/libexample_reqwest.so test.php

结果

Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Making async reqwest to https://httpbin.org/delay/5 that will return after 5 seconds...
Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~5 seconds!
Got response from https://httpbin.org/delay/5 after ~5 seconds!

有关此示例如何工作的更多信息,请参阅 源代码

使用php-tokio构建

以下是使用php-tokio构建的异步PHP扩展列表(通过编辑此文件添加您自己的!

依赖

~0.4–0.8MB
~19K SLoC