[][src]Struct actix::sync::SyncArbiter

pub struct SyncArbiter<A> where
    A: Actor<Context = SyncContext<A>>, 
{ /* fields omitted */ }

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.

Example

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::run(|| {
        // 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));
        }

    });
}

Methods

impl<A> SyncArbiter<A> where
    A: Actor<Context = SyncContext<A>>, 
[src]

pub fn start<F>(threads: usize, factory: F) -> Addr<A> where
    F: Fn() -> A + Send + Sync + 'static, 
[src]

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]

type Context = Context<Self>

Actor execution context type

fn started(&mut self, ctx: &mut Self::Context)[src]

Called when an actor gets polled the first time.

fn stopping(&mut self, ctx: &mut Self::Context) -> Running[src]

Called after an actor is in Actor::Stopping state. Read more

fn stopped(&mut self, ctx: &mut Self::Context)[src]

Called after an actor is stopped. Read more

fn start(self) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>, 
[src]

Start a new asynchronous actor, returning its address. Read more

fn start_default() -> Addr<Self> where
    Self: Actor<Context = Context<Self>> + Default
[src]

Construct and start a new asynchronous actor, returning its address. Read more

fn start_in_arbiter<F>(arb: &Arbiter, f: F) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>,
    F: FnOnce(&mut Context<Self>) -> Self + Send + 'static, 
[src]

Start new actor in arbiter's thread.

fn create<F>(f: F) -> Addr<Self> where
    Self: Actor<Context = Context<Self>>,
    F: FnOnce(&mut Context<Self>) -> Self + 'static, 
[src]

Start a new asynchronous actor given a Context. Read more

Auto Trait Implementations

impl<A> Send for SyncArbiter<A>

impl<A> Sync for SyncArbiter<A>

Blanket Implementations

impl<F, A> WrapFuture<A> for F where
    A: Actor,
    F: Future
[src]

type Future = FutureWrap<F, A>

The future that this type can be converted into.

type Item = <F as Future>::Item

The item that the future may resolve with.

type Error = <F as Future>::Error

The error that the future may resolve with.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<F> IntoFuture for F where
    F: Future
[src]

type Future = F

The future that this type can be converted into.

type Item = <F as Future>::Item

The item that the future may resolve with.

type Error = <F as Future>::Error

The error that the future may resolve with.

impl<T> Erased for T