pub struct TaskManager<R> { /* private fields */ }Expand description
Manages asynchronous tasks submitted as micro jobs from synchronous code. This type essentially gives the multithreading, asynchronous superpowers to synchronous logic.
§Example Usage
use std::sync::{Arc};
use tokio::sync::RwLock as AsyncRwLock;
use rumtk_core::core::RUMResult;
use rumtk_core::strings::RUMString;
use rumtk_core::threading::threading_manager::{SafeTaskArgs, TaskItems, TaskManager};
use rumtk_core::{rumtk_create_task, };
let expected = vec![
RUMString::from("Hello"),
RUMString::from("World!"),
RUMString::from("Overcast"),
RUMString::from("and"),
RUMString::from("Sad"),
];
type TestResult = RUMResult<Vec<RUMString>>;
let mut queue: TaskManager<TestResult> = TaskManager::new(&5).unwrap();
let locked_args = AsyncRwLock::new(expected.clone());
let task_args = SafeTaskArgs::<RUMString>::new(locked_args);
let processor = rumtk_create_task!(
async |args: &SafeTaskArgs<RUMString>| -> TestResult {
let owned_args = Arc::clone(args);
let locked_args = owned_args.read().await;
let mut results = TaskItems::<RUMString>::with_capacity(locked_args.len());
for arg in locked_args.iter() {
results.push(RUMString::new(arg));
}
Ok(results)
},
task_args
);
queue.add_task::<_>(processor);
let results = queue.wait();
let mut result_data = Vec::<RUMString>::with_capacity(5);
for r in results {
for v in r.unwrap().result.clone().unwrap().iter() {
for value in v.iter() {
result_data.push(value.clone());
}
}
}
assert_eq!(result_data, expected, "Results do not match expected!");
Implementations§
Source§impl<R> TaskManager<R>
impl<R> TaskManager<R>
Sourcepub fn default() -> RUMResult<TaskManager<R>>
pub fn default() -> RUMResult<TaskManager<R>>
This method creates a [TaskQueue] instance using sensible defaults.
The threads field is computed from the number of cores present in system.
Sourcepub fn new(worker_num: &usize) -> RUMResult<TaskManager<R>>
pub fn new(worker_num: &usize) -> RUMResult<TaskManager<R>>
Creates an instance of [ThreadedTaskQueue<T, R>] in the form of [SafeThreadedTaskQueue<T, R>].
Expects you to provide the count of threads to spawn and the microtask queue size
allocated by each thread.
This method calls [Self::with_capacity()] for the actual object creation.
The main queue capacity is pre-allocated to [DEFAULT_QUEUE_CAPACITY].
Sourcepub async fn add_task_async<F>(&mut self, task: F) -> TaskID
pub async fn add_task_async<F>(&mut self, task: F) -> TaskID
Add a task to the processing queue. The idea is that you can queue a processor function and list of args that will be picked up by one of the threads for processing.
This is the async counterpart
Sourcepub fn wait(&mut self) -> TaskResults<R>
pub fn wait(&mut self) -> TaskResults<R>
See wait_async
Sourcepub fn wait_on_batch(&mut self, tasks: &TaskBatch) -> TaskResults<R>
pub fn wait_on_batch(&mut self, tasks: &TaskBatch) -> TaskResults<R>
Sourcepub fn wait_on(&mut self, task_id: &TaskID) -> TaskResult<R>
pub fn wait_on(&mut self, task_id: &TaskID) -> TaskResult<R>
See wait_on_async
Sourcepub async fn wait_on_async(&mut self, task_id: &TaskID) -> TaskResult<R>
pub async fn wait_on_async(&mut self, task_id: &TaskID) -> TaskResult<R>
This method waits until a queued task with TaskID has been processed from the main queue.
We poll the status of the task every DEFAULT_SLEEP_DURATION ms.
Upon completion,
- Return the result (TaskResults
).
This operation consumes the task.
§Note:
Results returned here are not guaranteed to be in the same order as the order in which
the tasks were queued for work. You will need to pass a type as T that automatically
tracks its own id or has a way for you to resort results.Sourcepub async fn wait_on_batch_async(&mut self, tasks: &TaskBatch) -> TaskResults<R>
pub async fn wait_on_batch_async(&mut self, tasks: &TaskBatch) -> TaskResults<R>
This method waits until a set of queued tasks with TaskID has been processed from the main queue.
We poll the status of the task every DEFAULT_SLEEP_DURATION ms.
Upon completion,
- We collect the results generated (if any).
- Return the list of results (TaskResults
).
§Note:
Results returned here are not guaranteed to be in the same order as the order in which
the tasks were queued for work. You will need to pass a type as T that automatically
tracks its own id or has a way for you to resort results.Sourcepub async fn wait_async(&mut self) -> TaskResults<R>
pub async fn wait_async(&mut self) -> TaskResults<R>
This method waits until all queued tasks have been processed from the main queue.
We poll the status of the main queue every DEFAULT_SLEEP_DURATION ms.
Upon completion,
- We collect the results generated (if any).
- We reset the main task and result internal queue states.
- Return the list of results (TaskResults
).
This operation consumes all the tasks.
§Note:
Results returned here are not guaranteed to be in the same order as the order in which
the tasks were queued for work. You will need to pass a type as T that automatically
tracks its own id or has a way for you to resort results.Sourcepub fn is_all_completed(&self) -> bool
pub fn is_all_completed(&self) -> bool
Check if all work has been completed from the task queue.
§Examples
§Sync Usage
use rumtk_core::threading::threading_manager::TaskManager;
let manager = TaskManager::<usize>::new(&4).unwrap();
let all_done = manager.is_all_completed();
assert_eq!(all_done, true, "Empty TaskManager reports tasks are not completed!");
pub async fn is_all_completed_async(&self) -> bool
Trait Implementations§
Source§impl<R: Clone> Clone for TaskManager<R>
impl<R: Clone> Clone for TaskManager<R>
Source§fn clone(&self) -> TaskManager<R>
fn clone(&self) -> TaskManager<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more