Re: [Twisted-Python] PB waitingForAnswers KeyError
On Jul 22, 2005, at 1:32 PM, William Waites wrote:
On Fri, Jul 22, 2005 at 09:19:50AM -1000, Bob Ippolito wrote:
I expect that you're using TwistedManager incorrectly. There doesn't appear to be anything wrong with the code you attached, but it also doesn't do anything on its own.
I'm trying to put together a minimal example that is supposed to work and doesn't.
How the bit driven by calls works; the embedded Python creates a thread and does something like:
d = routeServer.callRemote("lookup", "somesource", "somedest") rset = manager.getDeferred(d)
That's your problem, you can't call ANY Twisted code from anything but the reactor thread (the one that called interleave), unless explicitly stated that it's allowed to do so (callFromThread, for example). You've misunderstood what the example is trying to demonstrate, it is not a magical "invert twisted" box. It demonstrates how you can create a simple ostensibly single-threaded application using Twisted that has a blocking interface on top of Deferreds. It's similar to how you would use any of the networking code in the standard library, only that there's asynchronous goodness somewhere behind the scenes making everything tick. -bob
On Fri, Jul 22, 2005 at 02:19:25PM -1000, Bob Ippolito wrote:
You've misunderstood what the example is trying to demonstrate, it is not a magical "invert twisted" box.
A magical "invert twisted" box... How about something like this? def invert(f): def _i(f, *av, **kw) e = Event() result = [] def _s(res): result.append(res) e.set() def _f(): d = maybeDeferred(f, *av, **kw) d.addBoth(_s) reactor.callFromThread(_f, *av, **kw) e.wait() res = result[0] if isinstance(res, Exception): raise res return res return _i class InvertedClass: def fooMethod(self, ...): ... do twisted stuff fooMethod = invert(fooMethod) Maybe with magic getattr functions it could be made completely transparent... Not sure if it is worth the bother though. If I am careful to do this sort of manipulation to protect potentially dangerous code, it seems like the problems with pb disappear... Thanks for your insight. -w
or more correctly, def invert(f): def _i(*av, **kw) e = Event() result = [] def _s(res): result.append(res) e.set() def _f(): d = maybeDeferred(f, *av, **kw) d.addBoth(_s) reactor.callFromThread(_f) e.wait() res = result[0] if isinstance(res, Exception): raise res return res return _i should this function exist somewhere in twisted? -w
On Jul 22, 2005, at 7:56 PM, William Waites wrote:
or more correctly,
def invert(f): def _i(*av, **kw) e = Event() result = [] def _s(res): result.append(res) e.set() def _f(): d = maybeDeferred(f, *av, **kw) d.addBoth(_s) reactor.callFromThread(_f) e.wait()
res = result[0] if isinstance(res, Exception): raise res return res return _i
should this function exist somewhere in twisted?
Maybe if you can provide a tested and documented implementation. I don't have a use for it, because I keep threads sane and separate, so I'm not going to think too hard about whether what you've correct is right or not. -bob
On Sat, 23 Jul 2005 01:56:29 -0400, William Waites <ww@groovy.net> wrote:
or more correctly,
def invert(f): def _i(*av, **kw) e = Event() result = [] def _s(res): result.append(res) e.set() def _f(): d = maybeDeferred(f, *av, **kw) d.addBoth(_s) reactor.callFromThread(_f) e.wait()
res = result[0] if isinstance(res, Exception): raise res return res return _i
should this function exist somewhere in twisted?
One might take the position that if you are not experience enough to write it yourself, you are not experience enough to write a multithreaded program. FWIW, this function has actually been written a handful of times, and at least a couple copies of it are in the list archives. There's also an open issue in the bug tracker related to it. I believe it is stalled on the submission of a reasonable set of unit tests. Jp
participants (3)
-
Bob Ippolito -
Jp Calderone -
William Waites