pub struct SyncPeriodicTasks { /* private fields */ }Expand description
A main instance which spawns the task executor.
let spt = SyncPeriodicTasks::new(1.try_into().unwrap()).unwrap();
let task1 = TaskStruct1::new(2, send);
let task1_ptt = PeriodicTaskTime::Relative(TimerExpMode::<RelativeTime>::new_interval(RelativeTime::new_time(1, 0)));
let task1_guard = s.add("task1", task1, task1_ptt).unwrap();This instance should be kept somewhere and dropped only if the task executor WITH all spawned tasks are no longer needed.
Implementations§
Source§impl SyncPeriodicTasks
impl SyncPeriodicTasks
Sourcepub fn new(threads_cnt: NonZeroUsize) -> TimerResult<Self>
pub fn new(threads_cnt: NonZeroUsize) -> TimerResult<Self>
Creates new instance. An amount of threads allocated for the task executor should be specified. All threads will be started immidiatly. For small tasks one thread will be enough. For a large amount of tasks, especially it tasks are waken up oftenly then at least two threads should be allocated.
§Arguments
threads_cnt- a NonZeroUsize amount of threads.
§Returns
The Result is returned as alias TimerResult.
Sourcepub fn add<T>(
&self,
task_name: impl Into<String>,
task: T,
task_time: PeriodicTaskTime,
) -> TimerResult<PeriodicTaskGuard>where
T: PeriodicTask,
pub fn add<T>(
&self,
task_name: impl Into<String>,
task: T,
task_time: PeriodicTaskTime,
) -> TimerResult<PeriodicTaskGuard>where
T: PeriodicTask,
Adds and spawns the task. The task is defined with generic T which is any
datatype which implements PeriodicTask.
§Arguments
-
task_name- a task name. used only for identification purposes in debug messages. -
task- a task which should be executed. It should implenet PeriodicTask. -
task_time- PeriodicTaskTime a time when the task must be spawned.
§Returns
The Result is returned as alias TimerResult.
§Example
#[derive(Debug)]
struct TaskStruct1
{
a1: u64,
s: Sender<u64>,
}
impl TaskStruct1
{
fn new(a1: u64, s: Sender<u64>) -> Self
{
return Self{ a1: a1, s };
}
}
impl PeriodicTask for TaskStruct1
{
fn exec(&mut self) -> PeriodicTaskResult
{
println!("taskstruct1 val: {}", self.a1);
let _ = self.s.send(self.a1);
return PeriodicTaskResult::Ok;
}
}
fn main()
{
let s = SyncPeriodicTasks::new(1.try_into().unwrap()).unwrap();
let (send, recv) = mpsc::channel::<u64>();
let task1 = TaskStruct1::new(2, send);
let task1_ptt = PeriodicTaskTime::exact_time(AbsoluteTime::now() + RelativeTime::new_time(3, 0));
let task1_guard = s.add("task1", task1, task1_ptt).unwrap();
println!("added");
let val = recv.recv();
println!("{:?}", val);
drop(task1_guard);
}Sourcepub fn add_closure<F>(
&self,
task_name: impl Into<String>,
task_time: PeriodicTaskTime,
clo: F,
) -> TimerResult<PeriodicTaskGuard>
pub fn add_closure<F>( &self, task_name: impl Into<String>, task_time: PeriodicTaskTime, clo: F, ) -> TimerResult<PeriodicTaskGuard>
Adds and spawns the task in form of a closure.
§Arguments
-
task_name- a task name. used only for identification purposes in debug messages. -
task_time- PeriodicTaskTime a time when the task must be spawned. -
clo- a function to exec on timeout. The function must return PeriodicTaskResult.
§Returns
The Result is returned as alias TimerResult.
§Example
let task1_ptt = PeriodicTaskTime::exact_time(AbsoluteTime::now() + RelativeTime::new_time(3, 0));
let (send, recv) = mpsc::channel::<u64>();
let task1_guard =
s.add_closure("task2", task1_ptt,
move ||
{
println!("test output");
send.send(1).unwrap();
return PeriodicTaskResult::Ok;
}
).unwrap();
let val = recv.recv();
println!("{:?}", val);Sourcepub fn check_thread_status(&self) -> Option<String>
pub fn check_thread_status(&self) -> Option<String>
Checks if any thread have crashed and no longer works.
§Returns
A Option is retuerned with the inner data:
-
Option::Some with the thread name String that have quit.
-
Option::None indicating that everthing is fine.
Trait Implementations§
Source§impl Clone for SyncPeriodicTasks
impl Clone for SyncPeriodicTasks
Source§fn clone(&self) -> SyncPeriodicTasks
fn clone(&self) -> SyncPeriodicTasks
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more