rustasync / runtime

Empowering everyone to build asynchronous software

Home Page:https://docs.rs/runtime

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to call an async function from a normal function?

ufosky opened this issue · comments

commented

I found if I use an async function, the other codes using this function must implement as an async function. Is there some suggestion for using async function from normal function?

Either use

futures::executor::block_on(your_future)

or

use runtime_tokio::Tokio;

fn main() {
    runtime::raw::enter(Tokio, your_future);
}

If you want to run it using this crate.

commented

We could / should probably expose some convenience methods here, so nobody ever needs to use runtime::raw directly:

with #[runtime::main]

runtime::task::block_on(async move {
    /* code goes here */
})

without #[runtime::main]

runtime_tokio::Tokio::block_on(async move {
    /* code goes here */
})
commented

Related: #42