Twisted:延迟子进程和http请求之间进行通信?
背景:
我有一个长时间运行的子进程,它扭曲了生成。 我以这样的方式对进程协议进行了子类化,当我收到 std out、std err 等时我会意识到 我希望能够通过共享变量或全局类或其他方式进行单独的 http 请求(在一系列时间内)来检查这些正在运行的进程的状态。
问题:
如何让子流程协议“丢弃”数据的事件侦听器以便稍后的 http 请求“拾取”。
Background:
I have a long running sub-process that twisted spawns.
I have subclassed the process protocol in such a way that I'm aware when I receive std out, std err, etc
I would like the ability for separate http requests (over a series of time) to check on the status of these running processes through shared variables or a global class or something.
Question:
How do I have the event listener for the sub process protocol "drop off" data for a later http request to "pick up".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
制作一个“邮箱”对象;为了便于讨论,我们假设这是一个列表。这可以是字典、对象、文件、数据库;任何你想要的。适合您的应用程序的任何内容。
然后,当您实例化
ProcessProtocol
时,传递对邮箱的引用。当相关数据到达时,self.mailbox.append(relevantData)
。另外,将此对象的引用传递给响应这些请求的 HTTP 资源。然后,在
render_GET
中,relevantData = self.mailbox.pop()
。在 Twisted 中没有什么神奇的方法可以做到这一点。这完全取决于您的应用程序的许多不同因素以及您想要存储和管理这些数据的方式,而 Twisted 显然不负责这些。
您在这里真正问的问题实际上可以归结为“我有一个对象
a
(您的进程协议)和一个对象b
(您的HTTP资源)。如何让a
调用b
上的方法? 此常见问题解答以各种形式一遍又一遍地出现在 Twisted 社区中,但很难写下一个很好的可重复使用的答案,因为每个人都认为他们在问不同的问题 。Twisted 正在做的事情 - Twisted 正在做的所有事情,实际上 - 是将进程外部事件的发生(来自子进程、网络的数据)映射到方法调用中 在您的流程中,如何排列内部对象、如何在对象之间保留引用以及如何处理 Twisted 刚刚提供给您的数据,完全取决于您。为什么 Twisted 如此强大。当您学会从回调中获取错误代码时,您就该离开了:)。
Make a 'mailbox' object; for argument's sake, let's say it's a list. This could be a dictionary, or an object, or a file, or a database; whatever you want. Whatever's appropriate to your application.
Then when you instantiate your
ProcessProtocol
, pass a reference to the mailbox. When relevant data arrives,self.mailbox.append(relevantData)
.Also, pass a reference to this object to your HTTP resource that's responding to these requests. Then, in
render_GET
,relevantData = self.mailbox.pop()
.There's no magic way to do this in Twisted. It all depends on lots of different things about your application and the way you want to store and manage this data, which Twisted is explicitly not in charge of.
The question you're really asking here really just boils down to, "I have an object
a
(your process protocol), and an objectb
(your HTTP resource). How do I geta
to call a method onb
? This FAQ shows up in various forms in the Twisted community, over and over again, but it's very hard to write down a nice re-usable answer to it, because everyone thinks they're asking a different question.What Twisted is doing - all Twisted is ever doing, really - is mapping the occurrence of events outside of your process - data arriving from subprocesses, the network - into method calls in your process. How you arrange the objects on the inside, how you keep references between them, and what you do with the data that Twisted has just given you, is totally up to you. This architecture is why Twisted is so powerful. When you have learned to snatch the error code from the callback, it will be time for you to leave :).