如何在Rust中进行进程间通信(IPC)? [英] How do you do interprocess communication (IPC) in Rust?

查看:1107
本文介绍了如何在Rust中进行进程间通信(IPC)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是标准库的一部分吗?

Is there part of the standard library for this?

我一直在研究,但是我看不到任何可以立即实现的东西,或者Process上可以让您执行此操作的任何东西?

I've been digging around, but I can't see anything immediately obvious that implements it, or anything on Process that would let you do this?

我想念吗?还是我必须为此功能做一些C-wrapper工作?

Did I miss it? Or do I have to do some C-wrapper work for this functionality?

(如果是这样,序列化Send的对象并将其传递给另一个进程然后在其中反序列化是否安全"?这就是Send的意思吗?)

(if so, is it 'safe' to serialize an object that is Send and pass it another process, then deserialize it there? That's what Send means right?)

推荐答案

Rust中没有任何一种有福的方式可以进行进程间通信.您将需要使用所需的任何技术:管道,裸套接字(TCP或UDP),共享内存,nanomsg/ZeroMQ,等等.

There is no single blessed way to do interprocess communication in Rust. You will need to use whatever technology you want: pipes, bare sockets (TCP or UDP), shared memory, nanomsg/ZeroMQ, whatever.

Send并不意味着您可以序列化实现该对象的类型的对象并将其发送到另一个进程.这意味着数据可以安全地发送到另一个 thread (例如,它不包含对源线程堆栈的任何引用),并且与序列化无关.

Also Send does not mean that you can serialize an object of a type which implements it and send to another process. It means that the data can be safely sent to another thread (e.g. it does not contain any references into the stack of the source thread), and it is not related to serialization.

例如,IPC的基本形式之一是父进程和子进程之间的stdin/stdout管道. Process API允许您通过以下管道发送和接收数据:

For example, one of the basic forms of IPC are stdin/stdout pipes between parent and child processes. Process API allows you to send and receive data through these pipes:

use std::io::process::Command;

fn main() {
    let mut p = Command::new("tr").arg("a-z").arg("A-Z").spawn().unwrap();

    {
        let mut p_stdin = p.stdin.as_mut().unwrap();
        p_stdin.write_str("hello world").unwrap();
    }
    p.wait().unwrap().success();

    let response = p.stdout.as_mut().unwrap().read_to_string().unwrap();
    println!("Responded: {}", response);
}

或者您可以使用套接字,但是示例会很大.您可以在此处找到套接字API文档.

Or you can use sockets, but the example would be quite large. You can find sockets API documentation here.

这篇关于如何在Rust中进行进程间通信(IPC)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆