#multiprocessing #communication #seamless #synchronously #cross-process #crossmist

macro crossmist-derive

Efficient and seamless cross-process communication, both synchronously and asynchronously

8 releases (2 stable)

new 1.0.2 May 3, 2024
1.0.0 May 1, 2024
0.2.4 Jan 25, 2024
0.2.0 Dec 19, 2023
0.1.3 Jul 29, 2023

#6 in #multiprocessing

Download history 12/week @ 2024-01-11 2/week @ 2024-01-18 9/week @ 2024-01-25 17/week @ 2024-02-22 4/week @ 2024-02-29 6/week @ 2024-03-07 6/week @ 2024-03-14 13/week @ 2024-03-28 16/week @ 2024-04-04 16/week @ 2024-04-11 67/week @ 2024-04-25

101 downloads per month
Used in crossmist

MIT license

23KB
471 lines

crossmist

License: MIT docs.rs crates.io

crossmist provides efficient and seamless cross-process communication for Rust. It provides semantics similar to std::thread::spawn and single-producer single-consumer channels, both synchronously and asynchronously.

Installation

$ cargo add crossmist

Or add the following to your Cargo.toml:

crossmist = "1.0.2"

Documentation

Check out docs.rs.

Motivational examples

This crate allows you to easily perform computations in another process without creating a separate executable or parsing command line arguments manually. For example, the simplest example, computing a sum of several numbers in a one-shot subprocess, looks like this:

#[crossmist::main]
fn main() {
    println!("5 + 7 = {}", add.run(vec![5, 7]).unwrap());
}

#[crossmist::func]
fn add(nums: Vec<i32>) -> i32 {
    nums.into_iter().sum()
}

This crate also supports long-lived tasks with constant cross-process communication:

#[crossmist::main]
fn main() {
    let (mut ours, theirs) = crossmist::duplex().unwrap();
    add.spawn(theirs).expect("Failed to spawn child");
    for i in 1..=5 {
        for j in 1..=5 {
            println!("{i} + {j} = {}", ours.request(&vec![i, j]).unwrap());
        }
    }
}

#[crossmist::func]
fn add(mut chan: crossmist::Duplex<i32, Vec<i32>>) {
    while let Some(nums) = chan.recv().unwrap() {
        chan.send(&nums.into_iter().sum());
    }
}

Almost arbitrary objects can be passed between processes and across channels, including file handles, sockets, and other channels.

Dependencies

~1.5MB
~41K SLoC