Add ability to run async functions in blocking mode
This commit is contained in:
parent
91cc658d86
commit
69d71d6588
3 changed files with 19 additions and 2 deletions
12
src/utilities/async_block.rs
Normal file
12
src/utilities/async_block.rs
Normal file
|
@ -0,0 +1,12 @@
|
|||
use std::future::Future;
|
||||
|
||||
|
||||
pub fn run_async<T>(async_operation: impl Future<Output = T>) -> T {
|
||||
// Use `block_in_place` to ensure runtime is dropped in a blocking context
|
||||
tokio::task::block_in_place(|| {
|
||||
// Get the current Tokio runtime handle
|
||||
let handle = tokio::runtime::Handle::current();
|
||||
// Run the async operation using the handle and wait for it to complete
|
||||
handle.block_on(async_operation)
|
||||
})
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod responses;
|
||||
pub mod async_block;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue