#ipc #communication #process #channel #request-response #tokio #cross

ipc_channel_adapter

提供快速跨进程通信的实用工具

3个版本

0.1.2 2024年5月12日
0.1.1 2024年5月11日
0.1.0 2024年5月11日

6#cross

Download history 248/week @ 2024-05-05 759/week @ 2024-05-12 510/week @ 2024-05-19 255/week @ 2024-06-02 122/week @ 2024-06-09 12/week @ 2024-06-16 1/week @ 2024-06-23 1/week @ 2024-07-07 1/week @ 2024-07-28 77/week @ 2024-08-04

每月 78 次下载
mach_bundler_core 中使用

MIT 许可证

30KB
742 代码行

IPC Channel Adapter

此实用工具封装了ipc-channel以处理握手和标准通道的提供。

此实用工具还提供消息的请求/响应包装器

它支持Tokio进行非阻塞操作

使用方法

父进程

fn main() {
  // Send requests to child
  //   Request to send to child is u32 
  //   Response coming back from child u64 
  let child_sender = ChildSender::<u32, u64>::new().unwrap();

  // Receive requests from child
  //   Request coming from child is u32 
  //   Response to send back to child is u64 
  let (child_receiver, child_rx) = ChildReceiver::<u32, u64>::new().unwrap();

  // Spawn child process and give it the names of the IPC channels
  let child_process = spawn_child_process("your_child_process")
    .env("IPC_RECEIVER_NAME", child_receiver.server_name)
    .env("IPC_SENDER_NAME", child_sender.server_name)
    .spawn();

  thread::spawn(move || {
    while let Ok((v, reply)) = child_rx.recv() {
      println!("[Host] Received: {}", v);
      reply.send(v).unwrap()
    }
  });

  let response = child_sender.send_blocking(42);
  println!("[Host] Response: {}", response);
}

子进程

fn main() {
  // Get name of IPC servers
  let host_receiver_server = env::var("IPC_RECEIVER_NAME").unwrap();
  let host_sender_server = env::var("IPC_SENDER_NAME").unwrap();
  
  // Send requests to host
  //   Request to send to host is u32 
  //   Response coming back from host u64 
  let host_sender = HostSender::<u32, u64>::new(&host_receiver_server).unwrap();

  // Receive requests from host
  //   Request coming from host is u32 
  //   Response to send back to host is u64 
  let (_host_receiver, host_receiver_rx) = HostReceiver::<u32, u64>::new(&host_sender_server).unwrap();

  thread::spawn(move || {
    while let Ok((v, reply)) = host_receiver_rx.recv() {
      println!("[Child] Received: {}", v);
      reply.send(v).unwrap()
    }
  });

  let response = host_sender.send_blocking(43);
  println!("[Child] Response: {}", response);
}

依赖关系

~3–34MB
~498K SLoC