Struct interruptible_polling::PollingTask
source · pub struct PollingTask { /* private fields */ }
Expand description
General purpose RAII polling task that executes a closure with a given frequency.
When PollingTask
is dropped, the background thread is signaled to perform a clean exit at
the first available opportunity. If the thread is currently sleeping, this will occur almost
immediately. If the closure is still running, it will happen immediately after the closure
finishes. The task joins on the background thread as a best-effort clean exit.
Note nothing special is done to try and keep the thread alive longer. If you terminate the program the default behavior of reaping the thread mid-execution will still occur.
Implementations§
source§impl PollingTask
impl PollingTask
sourcepub fn new<F>(interval: Duration, task: F) -> Result<Self, TryFromIntError>
pub fn new<F>(interval: Duration, task: F) -> Result<Self, TryFromIntError>
Creates a new background thread that immediately executes the given task.
interval
The interval to poll at. Note it must be expressible as an u64 in milliseconds.task
The closure to execute at every poll.
sourcepub fn new_with_checker<F>(
interval: Duration,
task: F
) -> Result<Self, TryFromIntError>
pub fn new_with_checker<F>( interval: Duration, task: F ) -> Result<Self, TryFromIntError>
Creates a new background thread that immediately executes the given task.
interval
The interval to poll at. Note it must be expressible as an u64 in milliseconds.task
The closure to execute at every poll. This closure gets access to another function that can assert if the managed task is still active.
If your task is long-running or has iterations (say updating 10 cache entries sequentially), you can assert if the managed task is active to early exit during a clean exit.
sourcepub fn set_polling_rate(
&self,
interval: Duration
) -> Result<(), TryFromIntError>
pub fn set_polling_rate( &self, interval: Duration ) -> Result<(), TryFromIntError>
Update the delay between poll events. Applied on the next iteration.
interval
The interval to poll at. Note it must be expressible as an u64 in milliseconds.