使用带有文件描述符的 Python Twisted 的示例

发布于 2024-08-18 03:19:16 字数 183 浏览 6 评论 0原文

我希望使用twisted 来控制主进程和一组从进程之间跨Linux 管道(os.pipe()) 和fifos (os.mkfifo()) 的通信。虽然我确信可以将twisted 用于这些类型的文件描述符(毕竟,twisted 对于 *nix 抽象为文件描述符的 tcp 套接字非常有用),但我找不到此类用法的任何示例。有人有任何链接、示例代码或建议吗?

I'm looking to use twisted to control communication across Linux pipes (os.pipe()) and fifos (os.mkfifo()) between a master process and a set of slave processes. While I'm positive tat it's possible to use twisted for these types of file descriptors (after all, twisted is great for tcp sockets which *nix abstracts away as file descriptors), I cannot find any examples of this type of usage. Anyone have any links, sample code, or advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-08-25 03:19:16

您可以使用reactor.spawnProcess在父进程和它生成的子进程之间设置任意文件描述符映射。例如,要运行一个程序并给它两个额外的输出描述符(除了 stdin、stdout 和 stderr),它可以使用它们将字节发送回父进程,您将执行如下操作

reactor.spawnProcess(protocol, executable, args,
                     childFDs={0: 'w', 1: 'r', 2: 'r', 3: 'r', 4: 'r'})

:为您创建管道,并在从管道读取数据时对您传入的 ProcessProtocol 调用 childDataReceived。有关详细信息,请参阅 spawnProcess API 文档

如果您还在子端使用 Twisted,那么您最想查看的是 twisted.internet.stdio核心示例中的 stdiodemo.py 和 stdin.py 将向您展示如何使用该模块。

You can use reactor.spawnProcess to set up arbitrary file descriptor mappings between a parent process and a child process it spawns. For example, to run a program and give it two extra output descriptors (in addition to stdin, stdout, and stderr) with which it can send bytes back to the parent process, you would do something like this:

reactor.spawnProcess(protocol, executable, args,
                     childFDs={0: 'w', 1: 'r', 2: 'r', 3: 'r', 4: 'r'})

The reactor will take care of creating the pipes for you, and will call childDataReceived on the ProcessProtocol you pass in when data is read from them. See the spawnProcess API docs for details.

If you're also using Twisted on the child end, then you mostly want to be looking at twisted.internet.stdio. stdiodemo.py and stdin.py in the core examples will show you how to use that module.

雨的味道风的声音 2024-08-25 03:19:16

它没有任何内置的异步 I/O。有人为它编写了一个 libaio 包装器,但尚未被触及很长一段时间了,不知道它是否仍然有效。

在最坏的情况下,您可以使用 select 来查看是否有任何内容可供阅读,但这对您的写作没有帮助。

It does not have anything built-in for asynchronous I/O. Someone wrote a libaio wrapper for it, but it has not been touched for a long time, and I have no idea if it still works.

In the worst case you could use select to see if there's anything available to read, but that won't help you with writing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文