[Twisted-Python] deferred folies
Federico Di Gregorio
fog at initd.org
Tue Sep 30 12:27:43 EDT 2003
hi *,
lately i did some dirty Deferred tricks and i just want to know if there
is a better way to do what i did. the situation is as follows:
a server exposes through pb a "session" object. the client can call on
that object "execute" (any number of times) and then "abort" or "end" to
end the session. the session is created by calling "new_session" on the
remote perspective (after authentication, etc.)
now, for a simple get session/execute/end sequence i don't want to write
n+1 callbacks (one that waits for new_session and call execute, next one
that wait on execute and call next, etc.) so here is what I did (inside
a mixin class used in the client):
def session(self):
return self.server.callRemote("new_session")
def execute(self, trans, *args, **kwargs)
cb = kwargs.get('callback', None)
if self._session:
dfr = self.server.callRemote(trans, *args)
if cb:
dfr.chainDeferred(cb)
return dfr
else:
dfr = self.session()
cb = Deferred()
dfr.addCallback(self._execute_got_session, trans, cb, *args)
return cb
def _execute_got_session(self, session, trans, cb, *args):
self._session = session
return self.execute(trans, *args, callback=cb)
that is, from the client the user can just do:
d = self.execute("set-foo", 42)
d.addCallback(...)
and in the callback call .execute again (and be sure to be in the same
session) or call end or abort to terminate the session.
clear? mm..
what i want to know is if the "cb" trick and chainDeferred call are a
dirty hack or if that's the way they are supposed to be used and if
there is a better way to do what i do.
ciao,
federico
--
Federico Di Gregorio
Debian GNU/Linux Developer fog at debian.org
INIT.D Developer fog at initd.org
Lasciate che i furetti vengano a me. -- Maria Luisa Benedetta Panzani
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Questa parte del messaggio =?ISO-8859-1?Q?=E8?= firmata
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030930/3a2d8722/attachment.pgp
More information about the Twisted-Python
mailing list