This project provide a ffrt-binding and napi-ext for ffrt.
Note: Don't use it to replace tokio directly, it only works for some simple scenarios now.
cargo add ohos-ffrt
# or
cargo add ohos-extWe can use it as another thread.
use napi_derive_ohos::napi;
use ohos_ffrt::Task;
#[napi]
pub fn run_ffrt() -> () {
let task = Task::new(Default::default());
task.submit(|| {
ohos_hilog_binding::hilog_info!("Hello, FFRT!");
});
}We can also define async function for napi with ohos-ext.
use ohos_ext::*;
#[napi(ts_return_type = "Promise<void>")]
pub fn example_a<'env>(
env: &'env Env,
callback: Function<FnArgs<(u32, u32, u32)>, String>,
) -> napi_ohos::Result<PromiseRaw<'env, ()>> {
let tsfn = callback.build_threadsafe_function().build()?;
// Use new method
env.spawn_local(async move {
let msg = tsfn.call_local((1, 2, 3).into()).await?;
ohos_hilog_binding::hilog_info!("msg: {}", msg);
Ok(())
})
}use ohos_ext::*;
#[ffrt]
pub async fn example_e() -> napi_ohos::Result<String> {
Ok("Hello, World!".to_string())
}You can see it as a built-in ThreadPool or async runtime. See detail with ffrt-kit.
We typically rely on tokio as the runtime for asynchronous tasks, but it adds extra overhead in terms of package size and startup thread load. Switching to ffrt helps address these challenges.