pub struct SyncArbiter<A>where
A: Actor<Context = SyncContext<A>>,{ /* private fields */ }
Expand description
SyncArbiter
provides the resources for a single Sync Actor to run on a dedicated
thread or threads. This is generally used for CPU bound concurrent workloads. It’s
important to note, that the SyncArbiter
generates a single address for the pool
of hosted Sync Actors. Any message sent to this Address, will be operated on by
a single Sync Actor from the pool.
Sync Actors have a different lifecycle compared to Actors on the System
Arbiter. For more, see SyncContext
.
§Examples
use actix::prelude::*;
struct Fibonacci(pub u32);
struct SyncActor;
impl Actor for SyncActor {
// It's important to note that you use "SyncContext" here instead of "Context".
type Context = SyncContext<Self>;
}
impl Handler<Fibonacci> for SyncActor {
type Result = Result<u64, ()>;
fn handle(&mut self, msg: Fibonacci, _: &mut Self::Context) -> Self::Result {
if msg.0 == 0 {
Err(())
} else if msg.0 == 1 {
Ok(1)
} else {
let mut i = 0;
let mut sum = 0;
let mut last = 0;
let mut curr = 1;
while i < msg.0 - 1 {
sum = last + curr;
last = curr;
curr = sum;
i += 1;
}
Ok(sum)
}
}
}
fn main() {
System::new().block_on(async {
// Start the SyncArbiter with 2 threads, and receive the address of the Actor pool.
let addr = SyncArbiter::start(2, || SyncActor);
// send 5 messages
for n in 5..10 {
// As there are 2 threads, there are at least 2 messages always being processed
// concurrently by the SyncActor.
addr.do_send(Fibonacci(n));
}
});
}
Implementations§
Source§impl<A> SyncArbiter<A>where
A: Actor<Context = SyncContext<A>>,
impl<A> SyncArbiter<A>where
A: Actor<Context = SyncContext<A>>,
Sourcepub fn start<F>(threads: usize, factory: F) -> Addr<A>
pub fn start<F>(threads: usize, factory: F) -> Addr<A>
Start a new SyncArbiter
with specified number of worker threads.
Returns a single address of the started actor. A single address is
used to communicate to the actor(s), and messages are handled by
the next available Actor in the SyncArbiter
.
Sourcepub fn start_with_thread_builder<F, BF>(
threads: usize,
thread_builder_factory: BF,
factory: F,
) -> Addr<A>
pub fn start_with_thread_builder<F, BF>( threads: usize, thread_builder_factory: BF, factory: F, ) -> Addr<A>
Start a new SyncArbiter
with specified number of worker threads.
Each worker thread is spawned from the std::thread::Builder
returned by a new call to thread_builder_factory
.
Returns a single address of the started actor. A single address is
used to communicate to the actor(s), and messages are handled by
the next available Actor in the SyncArbiter
.
Trait Implementations§
Source§impl<A> Actor for SyncArbiter<A>where
A: Actor<Context = SyncContext<A>>,
impl<A> Actor for SyncArbiter<A>where
A: Actor<Context = SyncContext<A>>,
Source§type Context = Context<SyncArbiter<A>>
type Context = Context<SyncArbiter<A>>
Source§fn started(&mut self, ctx: &mut Self::Context)
fn started(&mut self, ctx: &mut Self::Context)
Source§fn stopping(&mut self, ctx: &mut Self::Context) -> Running
fn stopping(&mut self, ctx: &mut Self::Context) -> Running
Actor::Stopping
state. Read moreSource§fn start_in_arbiter<F>(wrk: &ArbiterHandle, f: F) -> Addr<Self>
fn start_in_arbiter<F>(wrk: &ArbiterHandle, f: F) -> Addr<Self>
Auto Trait Implementations§
impl<A> Freeze for SyncArbiter<A>
impl<A> !RefUnwindSafe for SyncArbiter<A>
impl<A> Send for SyncArbiter<A>
impl<A> Sync for SyncArbiter<A>
impl<A> Unpin for SyncArbiter<A>
impl<A> !UnwindSafe for SyncArbiter<A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f
. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
()
on completion and sends
its output to another future on a separate task. Read moreSource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.Source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.Source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§type IntoFuture = F
type IntoFuture = F
Source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
Source§impl<F, A> WrapFuture<A> for F
impl<F, A> WrapFuture<A> for F
Source§type Future = FutureWrap<F, A>
type Future = FutureWrap<F, A>
Source§fn into_actor(self, _: &A) -> <F as WrapFuture<A>>::Future
fn into_actor(self, _: &A) -> <F as WrapFuture<A>>::Future
ActorFuture