php-tokio - Use any async Rust library from PHP!
Created by Daniil Gentili (@danog).
This library allows you to use any async rust library from PHP, asynchronously.
It's fully integrated with revolt: this allows full compatibility with amphp, PSL and reactphp.
Example
Here's an example, using the async Rust reqwest library to make asynchronous HTTP requests from PHP:
Usage:
&& \
&& \
&& \
Result:
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!
See the source code of the example for more info on how it works!
Usage
In your extension, make your async methods' class a #[php_async_impl], and expose EventLoop::init() (or EventLoop::init_with(), to choose the Tokio runtime) and EventLoop::wakeup(). The PHP side watches the stream that init() returns:
Don't close the stream: init() returns the same one every time. php-tokio shuts the event loop down at the end of every request (EventLoop::shutdown() does it earlier), once the futures borrowing from their callers completed.
Built with php-tokio
Here's a list of async PHP extensions built with php-tokio (add yours by editing this file!):
- nicelocal/mongo-php-async-driver - An async MongoDB PHP extension