#named-pipe #unix #fifo #async #path #ensure #named-pipe-path

unix-fifo-async

Unix 命名管道(FIFO)的异步包装器

1 个不稳定版本

0.0.3 2019 年 9 月 14 日

#22 in #named-pipe

MIT 许可证

14KB
203

Crates.io Build Status

unix-fifo-async

[WIP] 在文件系统上的任何位置简化使用 Unix 命名管道(FIFO)。

由于目前的工作方式,无法真正锁定管道,但在 NamedPipePathNamedPipeReader/NamedPipeWriter 上都有方便的方法来确保管道存在。

示例

创建一个管道,在一个异步任务中向其写入,在另一个任务中从中读取


use unix_fifo_async::NamedPipePath;
use async_std::task;

// Create a new pipe at the given path
let pipe = NamedPipePath::new("./my_pipe");
// This creates the path if it doesn't exist; it may return a nix::Error
// You can also use the `ensure_pipe_exists` convenience method on
// readers/writers, but calling it on both at the same time results
// in a race condition so it can never succeed.
pipe.ensure_exists().unwrap();
// Create a writer and a reader on the path
let writer = pipe.open_write();
let reader = pipe.open_read();

// Some data we can send over the pipe
let data_to_send = "Hello, pipes!";

// Spawn two tasks, one for writing to and one for reading from the pipe.
let t1 = task::spawn(async move { writer.write_str(data_to_send).await });
let t2 = task::spawn(async move { reader.read_string().await });

// `.await` both tasks and compare the result with the original
t1.await?;
let read_result = t2.await?;
assert_eq!(read_result, data_to_send);

// Delete the pipe
pipe.delete().await?;

请注意,在实际应用中,你可能希望从不同的进程或由完全不同的程序读取管道。

依赖项

~4.5MB
~83K SLoC