Struct tauri::async_runtime::Handle
pub struct Handle { /* fields omitted */ }
Expand description
Handle to the runtime.
The handle is internally reference-counted and can be freely cloned. A handle can be obtained using the Runtime::handle
method.
#
ImplementationsHandle#
implenter(&self) -> EnterGuard<'_>#
pub fnEnter the runtime context. This allows you to construct types that must have an executor available on creation such as Sleep
or TcpStream
. It will also allow you to call methods such as tokio::spawn
.
current() -> Handle#
pub fnReturns a Handle
view over the currently running Runtime
#
PanicThis will panic if called outside the context of a Tokio runtime. That means that you must call this on one of the threads being run by the runtime. Calling this from within a thread created by std::thread::spawn
(for example) will cause a panic.
#
ExamplesThis can be used to obtain the handle of the surrounding runtime from an async block or function running on that runtime.
use tokio::runtime::Handle;
// Inside an async block or function.
let handle = Handle::current();
handle.spawn(async {
println!("now running in the existing Runtime");
});
thread::spawn(move || {
// Notice that the handle is created outside of this thread and then moved in
handle.spawn(async { /* ... */ })
// This next line would cause a panic
// let handle2 = Handle::current();
});
try_current() -> Result<Handle, TryCurrentError>#
pub fnReturns a Handle view over the currently running Runtime
Returns an error if no Runtime has been started
Contrary to current
, this never panics
spawn<F>(&self, future: F) -> JoinHandle<<F as Future>::Output>ⓘNotable traits for JoinHandle<T>impl<T> Future for JoinHandle<T>type Output = Result<T, JoinError>;
where F: Future + Send + 'static, <F as Future>::Output: Send, <F as Future>::Output: 'static,#
pub fn Spawn a future onto the Tokio runtime.
This spawns the given future onto the runtime’s executor, usually a thread pool. The thread pool is then responsible for polling the future until it completes.
See module level documentation for more details.
#
Examplesuse tokio::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
// Get a handle from this runtime
let handle = rt.handle();
// Spawn a future onto the runtime using the handle
handle.spawn(async {
println!("now running on a worker thread");
});
spawn_blocking<F, R>(&self, func: F) -> JoinHandle<R>ⓘNotable traits for JoinHandle<T>impl<T> Future for JoinHandle<T>type Output = Result<T, JoinError>;
where R: Send + 'static, F: FnOnce() -> R + Send + 'static,#
pub fn Run the provided function on an executor dedicated to blocking operations.
#
Examplesuse tokio::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
// Get a handle from this runtime
let handle = rt.handle();
// Spawn a blocking function onto the runtime using the handle
handle.spawn_blocking(|| {
println!("now running on a worker thread");
});
block_on<F>(&self, future: F) -> <F as Future>::Output where F: Future,#
pub fnRun a future to completion on this Handle
’s associated Runtime
.
This runs the given future on the current thread, blocking until it is complete, and yielding its resolved result. Any tasks or timers which the future spawns internally will be executed on the runtime.
When this is used on a current_thread
runtime, only the Runtime::block_on
method can drive the IO and timer drivers, but the Handle::block_on
method cannot drive them. This means that, when using this method on a current_thread runtime, anything that relies on IO or timers will not work unless there is another thread currently calling Runtime::block_on
on the same runtime.
#
If the runtime has been shut downIf the Handle
’s associated Runtime
has been shut down (through Runtime::shutdown_background
, Runtime::shutdown_timeout
, or by dropping it) and Handle::block_on
is used it might return an error or panic. Specifically IO resources will return an error and timers will panic. Runtime independent futures will run as normal.
#
PanicsThis function panics if the provided future panics, if called within an asynchronous execution context, or if a timer future is executed on a runtime that has been shut down.
#
Examplesuse tokio::runtime::Runtime;
// Create the runtime
let rt = Runtime::new().unwrap();
// Get a handle from this runtime
let handle = rt.handle();
// Execute the future, blocking the current thread until completion
handle.block_on(async {
println!("hello");
});
Or using Handle::current
:
use tokio::runtime::Handle;
#[tokio::main]
async fn main () {
let handle = Handle::current();
std::thread::spawn(move || {
// Using Handle::block_on to run async code in the new thread.
handle.block_on(async {
println!("hello");
});
});
}
#
Trait ImplementationsClone for Handle#
implclone(&self) -> Handle#
pub fnReturns a copy of the value. Read more
clone_from(&mut self, source: &Self)1.0.0[src]#
fnPerforms copy-assignment from source
. Read more
Debug for Handle#
implfmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>#
pub fnFormats the value using the given formatter. Read more
RuntimeHandle for Handle[src]#
implspawn<F: Future>(&self, task: F) -> JoinHandle<F::Output>ⓘNotable traits for JoinHandle<T>impl<T> Future for JoinHandle<T>type Output = Result<T>;
where F: Future + Send + 'static, F::Output: Send + 'static,[src]#
fn Spawns a future onto the runtime.
block_on<F: Future>(&self, task: F) -> F::Output[src]#
fnRuns a future to completion on runtime.
#
Auto Trait ImplementationsRefUnwindSafe for Handle#
impl \!Send for Handle#
implSync for Handle#
implUnpin for Handle#
implUnwindSafe for Handle#
impl \!#
Blanket ImplementationsAny for T where T: 'static + ?Sized,[src]#
impl<T>type_id(&self) -> TypeId[src]#
pub fnGets the TypeId
of self
. Read more
Borrow<T> for T where T: ?Sized,[src]#
impl<T>borrow(&self) -> &T[src]#
pub fnImmutably borrows from an owned value. Read more
BorrowMut<T> for T where T: ?Sized,[src]#
impl<T>borrow_mut(&mut self) -> &mutT[src]#
pub fnMutably borrows from an owned value. Read more
From<T> for T[src]#
impl<T>from(t: T) -> T[src]#
pub fnPerforms the conversion.
Into<U> for T where U: From<T>,[src]#
impl<T, U>into(self) -> U[src]#
pub fnPerforms the conversion.
ToOwned for T where T: Clone,[src]#
impl<T>Owned = T#
typeThe resulting type after obtaining ownership.
to_owned(&self) -> T[src]#
pub fnCreates owned data from borrowed data, usually by cloning. Read more
clone_into(&self, target: &mutT)[src]#
pub fn🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
TryFrom<U> for T where U: Into<T>,[src]#
impl<T, U>Error = Infallible#
typeThe type returned in the event of a conversion error.
try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]#
pub fnPerforms the conversion.
TryInto<U> for T where U: TryFrom<T>,[src]#
impl<T, U>Error = <U as TryFrom<T>>::Error#
typeThe type returned in the event of a conversion error.
try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]#
pub fnPerforms the conversion.