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 wrapped function, sync ort async.
syncfunction will be executed in multiple system threadsasyncfunction will be executed intokiogreen 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 different ways to spawn an Actor:
-
To spawn sync
Actor-
Handle::spawn: spawnActorin1background thread -
Handle::spawn_n: spawnnActorinfnimplCloneinnbackground threads.
-
-
To spawn async
ActorHandle::spawn_tokio: spawnActorin background tokio thread, every asynchandlewill spawn an new tokio thread at background.
please check docs.rs for further infomation.
Handle
Handle can be connected together, build another type of Handle
use tokio;
use *;
let rt = new.unwrap.block_on;
Handle can spawn both async and sync actor at the same time
use tokio;
use *;
let rt = new.unwrap.block_on;
Please check examples/icecream.rs out for more complicated use case.