Struct actix::sync::SyncArbiter [−][src]
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
impl<A> SyncArbiter<A> where
A: Actor<Context = SyncContext<A>>,
[src]
A: Actor<Context = SyncContext<A>>,
pub fn start<F>(threads: usize, factory: F) -> Addr<A> where
F: Fn() -> A + Send + Sync + 'static,
[src]
F: Fn() -> A + Send + Sync + 'static,
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
.
Trait Implementations
impl<A> Actor for SyncArbiter<A> where
A: Actor<Context = SyncContext<A>>,
[src]
A: Actor<Context = SyncContext<A>>,
type Context = Context<Self>
Actor execution context type
fn started(&mut self, ctx: &mut Self::Context)
[src]
fn stopping(&mut self, ctx: &mut Self::Context) -> Running
[src]
fn stopped(&mut self, ctx: &mut Self::Context)
[src]
fn start(self) -> Addr<Self> where
Self: Actor<Context = Context<Self>>,
[src]
Self: Actor<Context = Context<Self>>,
fn start_default() -> Addr<Self> where
Self: Actor<Context = Context<Self>> + Default,
[src]
Self: Actor<Context = Context<Self>> + Default,
fn start_in_arbiter<F>(wrk: &ArbiterHandle, f: F) -> Addr<Self> where
Self: Actor<Context = Context<Self>>,
F: FnOnce(&mut Context<Self>) -> Self + Send + 'static,
[src]
Self: Actor<Context = Context<Self>>,
F: FnOnce(&mut Context<Self>) -> Self + Send + 'static,
fn create<F>(f: F) -> Addr<Self> where
Self: Actor<Context = Context<Self>>,
F: FnOnce(&mut Context<Self>) -> Self,
[src]
Self: Actor<Context = Context<Self>>,
F: FnOnce(&mut Context<Self>) -> Self,
Auto Trait Implementations
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> FutureExt for T where
T: Future + ?Sized,
[src]
T: Future + ?Sized,
pub fn map<U, F>(self, f: F) -> Map<Self, F> where
F: FnOnce(Self::Output) -> U,
[src]
F: FnOnce(Self::Output) -> U,
pub fn map_into<U>(self) -> MapInto<Self, U> where
Self::Output: Into<U>,
[src]
Self::Output: Into<U>,
pub fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F> where
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
[src]
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
pub fn left_future<B>(self) -> Either<Self, B> where
B: Future<Output = Self::Output>,
[src]
B: Future<Output = Self::Output>,
pub fn right_future<A>(self) -> Either<A, Self> where
A: Future<Output = Self::Output>,
[src]
A: Future<Output = Self::Output>,
pub fn into_stream(self) -> IntoStream<Self>
[src]
pub fn flatten(self) -> Flatten<Self> where
Self::Output: Future,
[src]
Self::Output: Future,
pub fn flatten_stream(self) -> FlattenStream<Self> where
Self::Output: Stream,
[src]
Self::Output: Stream,
pub fn fuse(self) -> Fuse<Self>
[src]
pub fn inspect<F>(self, f: F) -> Inspect<Self, F> where
F: FnOnce(&Self::Output),
[src]
F: FnOnce(&Self::Output),
pub fn unit_error(self) -> UnitError<Self>
[src]
pub fn never_error(self) -> NeverError<Self>
[src]
pub fn poll_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Self::Output> where
Self: Unpin,
[src]
Self: Unpin,
pub fn now_or_never(self) -> Option<Self::Output>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<F> IntoFuture for F where
F: Future,
[src]
F: Future,
type Output = <F as Future>::Output
into_future
)The output that the future will produce on completion.
type Future = F
into_future
)Which kind of future are we turning this into?
pub fn into_future(self) -> <F as IntoFuture>::Future
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<F, A> WrapFuture<A> for F where
F: Future,
A: Actor,
[src]
F: Future,
A: Actor,
type Future = FutureWrap<F, A>
The future that this type can be converted into.