Struct async_executors::exec::TokioTp
source · [−]pub struct TokioTp { /* private fields */ }
Expand description
An executor that uses tokio::runtime::Runtime.
Example
The following example shows how to pass an executor to a library function.
use
{
futures :: { task::{ Spawn, SpawnExt } } ,
async_executors :: { TokioTpBuilder } ,
tokio::runtime :: { Builder } ,
std::convert :: { TryFrom } ,
futures::channel :: { oneshot, oneshot::Sender } ,
};
fn lib_function( exec: impl Spawn, tx: Sender<&'static str> )
{
exec.spawn( async
{
tx.send( "I can spawn from a library" ).expect( "send string" );
}).expect( "spawn task" );
}
fn main()
{
// You must use the builder. This guarantees that TokioTp is always backed up by a threadpool.
// You can set other configurations by calling `tokio_builder()` on TokioTpBuilder, so you get
// access to the `tokio::runtime::Builder`.
//
let exec = TokioTpBuilder::new().build().expect( "create tokio threadpool" );
let program = async
{
let (tx, rx) = oneshot::channel();
lib_function( &exec, tx );
assert_eq!( "I can spawn from a library", rx.await.expect( "receive on channel" ) );
};
exec.block_on( program );
}
Unwind Safety.
You must only spawn futures to this API that are unwind safe. Tokio will wrap it in std::panic::AssertUnwindSafe and wrap the poll invocation with std::panic::catch_unwind.
They reason that this is fine because they require Send + 'static
on the future. As far
as I can tell this is wrong. Unwind safety can be circumvented in several ways even with
Send + 'static
(eg. parking_lot::Mutex
is Send + 'static
but !UnwindSafe
).
You should make sure that if your future panics, no code that lives on after the spawned task has unwound, nor any destructors called during the unwind can observe data in an inconsistent state.
If a future is run with block_on
as opposed to spawn
, the panic will not be caught and the
thread calling block_on
will be unwound.
Note that unwind safety is related to logic errors, not related to the memory safety issues that cannot happen in safe rust (memory safety, undefined behavior, unsoundness, data races, …). See the relevant catch_unwind RFC and it’s discussion threads for more info as well as the documentation of std::panic::UnwindSafe.
Implementations
sourceimpl TokioTp
impl TokioTp
sourcepub fn block_on<F: Future>(&self, f: F) -> F::Output
pub fn block_on<F: Future>(&self, f: F) -> F::Output
Forwards to Runtime::block_on.
sourcepub fn shutdown_timeout(self, duration: Duration) -> Result<(), Self>
pub fn shutdown_timeout(self, duration: Duration) -> Result<(), Self>
See: tokio::runtime::Runtime::shutdown_timeout
This tries to unwrap the Arc
sourcepub fn shutdown_background(self) -> Result<(), Self>
pub fn shutdown_background(self) -> Result<(), Self>
See: tokio::runtime::Runtime::shutdown_background
This tries to unwrap the Arc
Trait Implementations
sourceimpl<R: Send + 'static> SpawnBlocking<R> for TokioTp
impl<R: Send + 'static> SpawnBlocking<R> for TokioTp
sourcefn spawn_blocking<F>(&self, f: F) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
where
F: FnOnce() -> R + Send + 'static,
fn spawn_blocking<F>(&self, f: F) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
where
F: FnOnce() -> R + Send + 'static,
Runs the provided closure on a thread where blocking is acceptable.
sourcefn spawn_blocking_dyn(
&self,
f: Box<dyn FnOnce() -> R + Send>
) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
fn spawn_blocking_dyn(
&self,
f: Box<dyn FnOnce() -> R + Send>
) -> BlockingHandle<R>ⓘNotable traits for BlockingHandle<T>impl<T: 'static> Future for BlockingHandle<T> type Output = T;
Runs the provided closure on a thread where blocking is acceptable. This part of the trait is object safe but your closure must be boxed and you cannot have a return value. Read more
sourceimpl<Out: 'static + Send> SpawnHandle<Out> for TokioTp
impl<Out: 'static + Send> SpawnHandle<Out> for TokioTp
sourcefn spawn_handle_obj(
&self,
future: FutureObj<'static, Out>
) -> Result<JoinHandle<Out>, SpawnError>
fn spawn_handle_obj(
&self,
future: FutureObj<'static, Out>
) -> Result<JoinHandle<Out>, SpawnError>
Spawn a future and return a JoinHandle
that can be awaited for the output of the future.
sourceimpl YieldNow for TokioTp
impl YieldNow for TokioTp
sourcefn yield_now(&self) -> YieldNowFutⓘNotable traits for YieldNowFutimpl Future for YieldNowFut type Output = ();
fn yield_now(&self) -> YieldNowFutⓘNotable traits for YieldNowFutimpl Future for YieldNowFut type Output = ();
Await this future in order to yield to the executor.
Auto Trait Implementations
impl !RefUnwindSafe for TokioTp
impl Send for TokioTp
impl Sync for TokioTp
impl Unpin for TokioTp
impl !UnwindSafe for TokioTp
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more