[Twisted-Python] How to return a meaningful response to an AMP command when deferreds are involved?
On my AMP server I'm using twisted.enterprise to query a postgres database and do inserts. I'd like to return whether the insert succeeded or failed as the response to the AMP responder function -- is there an established pattern for doing things like that? So basically, with the amp command: class MyInsert(amp.Command): arguments = [('an_int',amp.Integer())] response = [('succeeded',amp.Boolean())] ....on the client I do: amp_client.callRemote(MyInsert, an_int) ...on the server: @MyInsert.responder def my_insert(self, an_int): qry_def = self.dbpool.runOperation("insert into mytable the_number values (%d);" % an_int) qry_def.addCallback(self._my_insert_ok) qry_def.addErrback(self._my_insert_err) # Here's the AMP response...but I don't KNOW whether it succeeded yet! # ...but I've _got_ to return a response dict from this function or AMP gives an error! return {'succeeded':True} def _my_insert_ok(self, none_object): print "It succeeded" def _my_insert_err(self, failure_object): print "It failed with: %s" % failure_object.getErrorMessage() ~ Nathan
On Wed, 2 Apr 2008 13:05:49 -0600, Nathan <nathan.stocks@gmail.com> wrote:
On my AMP server I'm using twisted.enterprise to query a postgres database and do inserts. I'd like to return whether the insert succeeded or failed as the response to the AMP responder function -- is there an established pattern for doing things like that?
If a responder returns a Deferred, then the result of the Deferred is taken as the response to the request.
So basically, with the amp command:
class MyInsert(amp.Command): arguments = [('an_int',amp.Integer())] response = [('succeeded',amp.Boolean())]
....on the client I do:
amp_client.callRemote(MyInsert, an_int)
...on the server:
@MyInsert.responder def my_insert(self, an_int): qry_def = self.dbpool.runOperation("insert into mytable the_number values (%d);" % an_int) qry_def.addCallback(self._my_insert_ok) qry_def.addErrback(self._my_insert_err) # Here's the AMP response...but I don't KNOW whether it succeeded yet! # ...but I've _got_ to return a response dict from this function or AMP gives an error! return {'succeeded':True}
Don't return that dict here. Return qry_def, instead. Make your callbacks return dicts or raise exceptions to indicate the result. Jean-Paul
On Wed, Apr 2, 2008 at 1:10 PM, Jean-Paul Calderone <exarkun@divmod.com> wrote:
On Wed, 2 Apr 2008 13:05:49 -0600, Nathan <nathan.stocks@gmail.com> wrote:
On my AMP server I'm using twisted.enterprise to query a postgres database and do inserts. I'd like to return whether the insert succeeded or failed as the response to the AMP responder function -- is there an established pattern for doing things like that?
If a responder returns a Deferred, then the result of the Deferred is taken as the response to the request.
Aha! Things are falling into place. I've run into this problems several times, and that's the piece that I had missed. Thanks a million! /me needs to go read the docs on deferreds for a fourth time... ~ Nathan
participants (2)
-
Jean-Paul Calderone -
Nathan