20 个版本

0.12.0 2021 年 3 月 10 日
0.11.1 2019 年 11 月 18 日
0.10.0 2019 年 7 月 9 日
0.9.0 2019 年 3 月 31 日
0.1.2 2015 年 11 月 2 日

#221内存管理

Download history 386/week @ 2024-03-13 332/week @ 2024-03-20 345/week @ 2024-03-27 344/week @ 2024-04-03 277/week @ 2024-04-10 353/week @ 2024-04-17 364/week @ 2024-04-24 278/week @ 2024-05-01 287/week @ 2024-05-08 298/week @ 2024-05-15 339/week @ 2024-05-22 348/week @ 2024-05-29 317/week @ 2024-06-05 337/week @ 2024-06-12 246/week @ 2024-06-19 144/week @ 2024-06-26

1,097 每月下载量
用于 13 个 crate(通过 timely

MIT 许可证

125KB
2K SLoC

提供类型化交换通道的简单通信基础设施。

这个 crate 是 timely 数据流系统的一部分,主要用于其工作进程间的通信。它可能独立使用,但主要是为了在项目中明确边界。

使用 allocator::Generic 生成线程,其 allocate 方法返回多个发送端点和单个接收端点。如果发送端点接收频繁,则最终会由对应的工作进程接收消息。点对点通道每个都是 FIFO,但没有公平性保证。

要通讯,类型必须在使用 bincode 功能时实现 Serialize 特性,或者在不使用时实现 Abomonation 特性。

通道端点还实现了更低级的 pushpull 接口(通过 PushPull 特性),用于更精确的资源控制。

示例

use timely_communication::Allocate;

// configure for two threads, just one process.
let config = timely_communication::Config::Process(2);

// initializes communication, spawns workers
let guards = timely_communication::initialize(config, |mut allocator| {
    println!("worker {} started", allocator.index());

    // allocates a pair of senders list and one receiver.
    let (mut senders, mut receiver) = allocator.allocate(0);

    // send typed data along each channel
    use timely_communication::Message;
    senders[0].send(Message::from_typed(format!("hello, {}", 0)));
    senders[1].send(Message::from_typed(format!("hello, {}", 1)));

    // no support for termination notification,
    // we have to count down ourselves.
    let mut expecting = 2;
    while expecting > 0 {

        allocator.receive();
        if let Some(message) = receiver.recv() {
            use std::ops::Deref;
            println!("worker {}: received: <{}>", allocator.index(), message.deref());
            expecting -= 1;
        }
        allocator.release();
    }

    // optionally, return something
    allocator.index()
});

// computation runs until guards are joined or dropped.
if let Ok(guards) = guards {
    for guard in guards.join() {
        println!("result: {:?}", guard);
    }
}
else { println!("error in computation"); }

应生成类似以下输出

worker 0 started
worker 1 started
worker 0: received: <hello, 0>
worker 1: received: <hello, 1>
worker 0: received: <hello, 0>
worker 1: received: <hello, 1>
result: Ok(0)
result: Ok(1)

依赖项

~1.3–2MB
~37K SLoC