Skip to content

How does Corece handle CPU-bound operations? #35

@lucasmsoares96

Description

@lucasmsoares96

I am migrating my project from actix to coerce and I have a question about how CPU-bound tasks are managed. The example below shows the actix code to perform the fibonacci operation on 3 Arbiters. An arbiter is a single-threaded event loop that runs one or more actors. In this way, although the event loop is blocked by the fibonacci calculation, it is possible to perform 3 calculations in parallel.

#[actix::main]
async fn main() {
    let start = Instant::now();
    let a1 = Arbiter::new().handle();
    let a2 = Arbiter::new().handle();
    let a3 = Arbiter::new().handle();

    let addr1 = Calculator::start_in_arbiter(&a1, |_ctx| Calculator);
    let addr2 = Calculator::start_in_arbiter(&a2, |_ctx| Calculator);
    let addr3 = Calculator::start_in_arbiter(&a3, |_ctx| Calculator);

    let fib_future1 = addr1.send(Fibonacci(43));
    let fib_future2 = addr2.send(Fibonacci(43));
    let fib_future3 = addr3.send(Fibonacci(43));

    let futures = vec![fib_future1, fib_future2, fib_future3];

    let results = join_all(futures).await;

    for (i, result) in results.iter().enumerate() {
        match result {
            Ok(value) => println!("Resultado {}: {}", i + 1, value),
            Err(e) => println!("Erro ao receber o resultado {}: {:?}", i + 1, e),
        }
    }

    let duration = start.elapsed();
    println!("Tempo decorrido: {:?}", duration);
}

How does Coerce work under the hood? I couldn't find anything about it in the documentation. How to perform 3 CPU-bound tasks in parallel as in the example?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions