pub struct ConcurrentTasks<I, O> { /* private fields */ }
Expand description
ConcurrentTasks is used to execute tasks concurrently.
ConcurrentTasks has two generic types:
I
represents the input type of the task.O
represents the output type of the task.
§Implementation Notes
The code patterns below are intentional; please do not modify them unless you fully understand these notes.
let (i, o) = self
.tasks
.front_mut() // Use `front_mut` instead of `pop_front`
.expect("tasks must be available")
.await;
...
match o {
Ok(o) => {
let _ = self.tasks.pop_front(); // `pop_front` after got `Ok(o)`
self.results.push_back(o)
}
Err(err) => {
if err.is_temporary() {
let task = self.create_task(i);
self.tasks
.front_mut()
.expect("tasks must be available")
.replace(task) // Use replace here to instead of `push_front`
} else {
self.clear();
self.errored = true;
}
return Err(err);
}
}
Please keep in mind that there is no guarantee the task will be await
ed until completion. It’s possible
the task may be dropped before it resolves. Therefore, we should keep the Task
in the tasks
queue until
it is resolved.
For example, users may have a timeout for the task, and the task will be dropped if it exceeds the timeout.
If we pop_front
the task before it resolves, the task will be canceled and the result will be lost.
Implementations§
Source§impl<I: Send + 'static, O: Send + 'static> ConcurrentTasks<I, O>
impl<I: Send + 'static, O: Send + 'static> ConcurrentTasks<I, O>
Sourcepub fn new(
executor: Executor,
concurrent: usize,
factory: fn(I) -> BoxedStaticFuture<(I, Result<O>)>,
) -> Self
pub fn new( executor: Executor, concurrent: usize, factory: fn(I) -> BoxedStaticFuture<(I, Result<O>)>, ) -> Self
Create a new concurrent tasks with given executor, concurrent and factory.
The factory is a function pointer that shouldn’t capture any context.
Sourcepub fn has_remaining(&self) -> bool
pub fn has_remaining(&self) -> bool
Check if there are remaining space to push new tasks.
Sourcepub fn has_result(&self) -> bool
pub fn has_result(&self) -> bool
Chunk if there are remaining results to fetch.
Sourcepub fn create_task(&self, input: I) -> Task<(I, Result<O>)>
pub fn create_task(&self, input: I) -> Task<(I, Result<O>)>
Create a task with given input.