Tokiactor
About Tokiactor
tokiactor is a minimized implementation of actor pattern based on tokio runtime. No concepts like or System or Context involved, just MessageHandle and Actor.
In tokiactor, Actor is a wrapper of sync and async function. sync function will be executed in multiple system threads, async function will be executed in tokio green thread asynchronously.
Large batch tasks like processing thousands of pictures can be done in parallel by leveraging buffered futures::StreamExt trait from crate futures.
Installation
Add tokiactor to Cargo.toml, tokiactor needs tokio to make things work.
[]
= "*"
Getting start
Following code will create Adder actor, then, actor spawned in Handle, Adder will be called thru Handle::handle method asynchronously.
use tokio;
use *;
let rt = new.unwrap.block_on;
or, we can create Actor from async Closure:
use tokio;
use *;
let rt = new.unwrap.block_on;
or, create Actor from blocking fn, then run Actor in parallel
use tokio;
use *;
use StreamExt;
let rt = new.unwrap.block_on;
Actor spawn
There are many different ways to spawn an Actor:
-
To spawn sync
Actor-
Actor::spawn: spawnActorin1background thread -
Actor::spawn_n: spawnnActorinfnimplCloneinnbackground threads. -
Actor::n_spawn: spawnnActorcreated already,nbackground threads.
-
-
To spawn async
Actor-
Actor::spawn_tokio: spawnActorin1background tokio thread -
Actor::spawn_n_tokio: spawnnActorinfnimplCloneinnbackground tokio threads. -
Actor::n_spawn_tokio: spawnnActorcreated before,nbackground tokio threads.
-
please check docs.rs for further infomation.
Handle<I, O>
Handle<I, O> can be connected together, build another type of Handle
use tokio;
use *;
let rt = new.unwrap.block_on;
Please check examples/icecream.rs out for more complicated use case.