Struct async_executors::exec::TokioTp[][src]

pub struct TokioTp { /* fields omitted */ }
This is supported on crate feature tokio_tp only.
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

Forwards to Runtime::block_on.

See: tokio::runtime::Runtime::shutdown_timeout

This tries to unwrap the Arc we hold, so that works only if no other clones are around. If this is not the only reference, self will be returned to you as an error. It means you cannot shutdown the runtime because there are other clones of the executor still alive.

See: tokio::runtime::Runtime::shutdown_background

This tries to unwrap the Arc we hold, so that works only if no other clones are around. If this is not the only reference, self will be returned to you as an error. It means you cannot shutdown the runtime because there are other clones of the executor still alive.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Spawns a future that will be run to completion. Read more

Determines whether the executor is able to spawn new tasks. Read more

Runs the provided closure on a thread where blocking is acceptable.

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

Spawn a future and return a JoinHandle that can be awaited for the output of the future.

Future that resolves after a given duration.

Await this future in order to yield to the executor.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Spawns a task that polls the given future with output () to completion. Read more

Spawns a task that polls the given future to completion and returns a future that resolves to the spawned future’s output. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more