pub struct Handle<E> { /* private fields */ }
Expand description
A handle to a running service loop.
You can use it to cancel the running loop at the next opportunity (through [Handle::cancel
]),
or to wait for the loop to terminate (through Handle::wait
). You can also use
Handle::canceller
to get a Canceller
handle, which lets you terminate the service loop
elsewhere (e.g., while waiting).
Implementations§
Source§impl<E> Handle<E>
impl<E> Handle<E>
Sourcepub fn canceller(&self) -> Canceller
pub fn canceller(&self) -> Canceller
Get another handle for cancelling the service loop.
This can be handy if you want one thread to wait for the service loop to exit, while another watches for exit signals.
Examples found in repository?
25fn main() {
26 let s = Service::new();
27 eprintln!("server running");
28 let h = s.spawn();
29 let exit = h.canceller();
30 thread::spawn(move || {
31 thread::sleep(time::Duration::from_secs(10));
32 eprintln!("server terminating");
33 exit.cancel();
34 });
35 h.wait().unwrap();
36 eprintln!("server terminated");
37}
Sourcepub fn wait(self) -> Result<(), E>
pub fn wait(self) -> Result<(), E>
Block the current thread waiting for the service loop to exit, and return its result.
If the service loop returns an error, this method will return it in the Err
value.
If the service loop panics, this method will also panic with the same error.
Examples found in repository?
25fn main() {
26 let s = Service::new();
27 eprintln!("server running");
28 let h = s.spawn();
29 let exit = h.canceller();
30 thread::spawn(move || {
31 thread::sleep(time::Duration::from_secs(10));
32 eprintln!("server terminating");
33 exit.cancel();
34 });
35 h.wait().unwrap();
36 eprintln!("server terminated");
37}
Methods from Deref<Target = Canceller>§
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancel the currently running service loop. This method does not block; it sends a signal that the service loop should cease execution and returns immediately.
Note that this will not interrupt a currently executing Cancellable::for_each
.
Instead, the next time Cancellable::for_each
would be called, the service loop will
return.
Examples found in repository?
25fn main() {
26 let s = Service::new();
27 eprintln!("server running");
28 let h = s.spawn();
29 let exit = h.canceller();
30 thread::spawn(move || {
31 thread::sleep(time::Duration::from_secs(10));
32 eprintln!("server terminating");
33 exit.cancel();
34 });
35 h.wait().unwrap();
36 eprintln!("server terminated");
37}