Skip to main content

set_interruption_handler

Function set_interruption_handler 

Source
pub fn set_interruption_handler(
    handler: impl FnMut() -> bool + 'static,
) -> InterruptionHandlerGuard
Expand description

Installs a Rust closure as the interruption handler of the calling thread until the returned guard is dropped.

igraph calls the interruption handler regularly during long computations (shortest paths, SIR simulations, layouts, random graph generators, …). The closure returns true to request the interruption of the running computation, which then fails with ErrorKind::Interrupted, and false to let it go on. Typical uses are timeouts, cancellation flags set by another thread (e.g. an Arc<AtomicBool>), or Ctrl-C handling. A panic in the closure interrupts the computation and is resumed afterwards.

The closure may call igraph, but igraph polls the handler from inside running computations, including the ARPACK eigensolvers (e.g. linalg::arpack_rssolve or Graph::eigenvector_centrality). igraph’s ARPACK is not re-entrant (it keeps its iteration state in thread-local statics), so a handler that may run during an ARPACK-based computation must not start an ARPACK-based computation of its own: every ARPACK-based function of the crate detects this and fails with an ErrorKind::Failure error instead of corrupting the running solver (see linalg). The same holds for progress handlers.

There is no default interruption handler. Binds igraph_set_interruption_handler (see igraph_interrupt.h).

§Examples

use igraph::prelude::*;
use igraph::misc;
use std::time::{Duration, Instant};

let deadline = Instant::now() + Duration::from_secs(3600);
let _guard = misc::set_interruption_handler(move || Instant::now() > deadline);
// Plenty of time: the simulation completes.
let g = Graph::from_edges(&[(0, 1), (1, 2)], 3, false)?;
assert_eq!(g.sir(1.0, 1.0, 3)?.len(), 3);