开发者

Twisted: Communicate between deferred sub-process and http requests?

开发者 https://www.devze.com 2023-02-09 00:30 出处:网络
Background: I have a long running sub-process that twisted spawns. I have subclassed the process prot开发者_运维问答ocol in such a way that I\'m aware when I receive std out, std err, etc

Background:

I have a long running sub-process that twisted spawns. I have subclassed the process prot开发者_运维问答ocol 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".


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 object b (your HTTP resource). How do I get a to call a method on b? 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 :).

0

精彩评论

暂无评论...
验证码 换一张
取 消