exarkun at divmod.com
Mon Nov 15 18:51:22 EST 2004
On Mon, 15 Nov 2004 14:49:13 -0800 (PST), Chad Rosenberg <chad at idrankwhat.net> wrote:
>I've run into a problem that requires some form of blocking. I've got a
> method that's compiled from a cheetah template and that gets called as the
> result of a reactor callback. The cheetah method needs to be able to make
> a PB call to fetch some data and block until the results arrive.
> Something like this:
> def reactorCallback(self):
> def cheetahCompiledMethod(self):
> print self.utilClass.lookup(foo)
> class UtilClass:
> def lookup(self, whatToLookup):
> pbDeferred = self.pbCall(whatToLookup)
> # Some way to block until results are gotten
> return results
> "cheetahCompiledMethod", itself, cannot use callbacks. The call to
> "lookup" can't be done before "cheetahCompiledMethod" is called since it
> is unknown at that time what "cheetahCompiledMethod" will require. I was
> looking over Christopher Armstrong's gthreadless.py
> (http://radix.twistedmatrix.com/archives/000099.html) and it looks
> promising, but I am a little concerned about the stability of greenlets.
> I also noticed twisted.trial.util.wait. What is the best way to handle
> situations like this when they come up?
The best way is to use callbacks and wait for the Deferred to fire.
If greenlets are stable, gthreadless may be the next best way, but I have not yet used them enough to be confident that they are stable.
threadwrapper.py is another candidate for a solution which is worse than using Deferreds.
twisted.trial.util.wait() is a very bad solution.
More information about the Twisted-Python