pub struct TaskManager { /* private fields */ }Expand description
Task manager is an asynchronous task supervisor that stores all spawned tasks, controls its states and provides an api for task management.
Implementations§
Source§impl TaskManager
impl TaskManager
Sourcepub fn builder() -> TaskManagerBuilder
pub fn builder() -> TaskManagerBuilder
Returns a task manager builder.
Examples found in repository?
23async fn main() {
24 let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26 let mut task_keys = Vec::new();
27 for i in 0..10 {
28 let task_key = task_manager.try_spawn(outer(i)).unwrap();
29 task_keys.push(task_key)
30 }
31
32 tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34 for task_key in task_keys {
35 if task_manager.cancel_task(task_key).is_ok() {
36 println!("task-{} canceled", task_key)
37 } else {
38 println!("task-{} already finished", task_key)
39 }
40 }
41
42 task_manager.join(true).await;
43}Sourcepub fn new(
max_tasks: usize,
capacity: usize,
completion_events_buffer_size: usize,
) -> TaskManager
pub fn new( max_tasks: usize, capacity: usize, completion_events_buffer_size: usize, ) -> TaskManager
Creates a new task manager instance.
Sourcepub fn try_spawn<F>(&mut self, future: F) -> Option<usize>
pub fn try_spawn<F>(&mut self, future: F) -> Option<usize>
Spawns a new asynchronous task wrapping it to be supervised by the task manager.
Method can return None if task manager is full and task can not be spawned yet
otherwise it returns task key that can be used to cancel this task.
Examples found in repository?
23async fn main() {
24 let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26 let mut task_keys = Vec::new();
27 for i in 0..10 {
28 let task_key = task_manager.try_spawn(outer(i)).unwrap();
29 task_keys.push(task_key)
30 }
31
32 tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34 for task_key in task_keys {
35 if task_manager.cancel_task(task_key).is_ok() {
36 println!("task-{} canceled", task_key)
37 } else {
38 println!("task-{} already finished", task_key)
39 }
40 }
41
42 task_manager.join(true).await;
43}Sourcepub async fn process(&mut self, resume_panic: bool)
pub async fn process(&mut self, resume_panic: bool)
Runs manager processing loop handling task events.
Method is cancellation safe and can be used in tokio::select! macro.
If resume_panic argument is true and any of the tasks panic
method resumes the panic on the current task. It is useful in test environment
when you want your application to be panicked if any of the spawned tasks panic.
Examples found in repository?
23async fn main() {
24 let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26 let mut task_keys = Vec::new();
27 for i in 0..10 {
28 let task_key = task_manager.try_spawn(outer(i)).unwrap();
29 task_keys.push(task_key)
30 }
31
32 tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34 for task_key in task_keys {
35 if task_manager.cancel_task(task_key).is_ok() {
36 println!("task-{} canceled", task_key)
37 } else {
38 println!("task-{} already finished", task_key)
39 }
40 }
41
42 task_manager.join(true).await;
43}Sourcepub fn detach(
&mut self,
task_key: usize,
) -> Result<TaskHandle<()>, TaskManagerError>
pub fn detach( &mut self, task_key: usize, ) -> Result<TaskHandle<()>, TaskManagerError>
Detaches a task from the manager. The task is not longer supervised by the manager.
Sourcepub async fn join(self, resume_panic: bool)
pub async fn join(self, resume_panic: bool)
Waits until all the tasks are completed consuming self.
If resume_panic argument is true and any of the tasks panic
method resumes the panic on the current task. It is useful in test environment
when you want your application to be panicked if any of the spawned tasks panic.
Examples found in repository?
23async fn main() {
24 let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26 let mut task_keys = Vec::new();
27 for i in 0..10 {
28 let task_key = task_manager.try_spawn(outer(i)).unwrap();
29 task_keys.push(task_key)
30 }
31
32 tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34 for task_key in task_keys {
35 if task_manager.cancel_task(task_key).is_ok() {
36 println!("task-{} canceled", task_key)
37 } else {
38 println!("task-{} already finished", task_key)
39 }
40 }
41
42 task_manager.join(true).await;
43}Sourcepub fn cancel_task(&mut self, task_key: usize) -> Result<(), TaskManagerError>
pub fn cancel_task(&mut self, task_key: usize) -> Result<(), TaskManagerError>
Cancels a particular task by task key returned by TaskManager::try_spawn method.
If task not found (task key is wrong or task already finished)
method returns TaskManagerError::TaskNotFound error.
Examples found in repository?
23async fn main() {
24 let mut task_manager = cm::TaskManager::builder().with_max_tasks(10).with_capacity(10).build();
25
26 let mut task_keys = Vec::new();
27 for i in 0..10 {
28 let task_key = task_manager.try_spawn(outer(i)).unwrap();
29 task_keys.push(task_key)
30 }
31
32 tokio::time::timeout(time::Duration::from_secs(5), task_manager.process(false)).await;
33
34 for task_key in task_keys {
35 if task_manager.cancel_task(task_key).is_ok() {
36 println!("task-{} canceled", task_key)
37 } else {
38 println!("task-{} already finished", task_key)
39 }
40 }
41
42 task_manager.join(true).await;
43}Sourcepub fn abort_task(&mut self, task_key: usize) -> Result<(), TaskManagerError>
pub fn abort_task(&mut self, task_key: usize) -> Result<(), TaskManagerError>
Aborts a task by a task key. The task is removed from the storage so that it can’t be accessed anymore.