使用带有文件描述符的 Python Twisted 的示例
我希望使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用reactor.spawnProcess在父进程和它生成的子进程之间设置任意文件描述符映射。例如,要运行一个程序并给它两个额外的输出描述符(除了 stdin、stdout 和 stderr),它可以使用它们将字节发送回父进程,您将执行如下操作
:为您创建管道,并在从管道读取数据时对您传入的
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:The reactor will take care of creating the pipes for you, and will call
childDataReceived
on theProcessProtocol
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.它没有任何内置的异步 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.