[Twisted-Python] How to force synchronous behavior

Jean-Paul Calderone exarkun at divmod.com
Mon Oct 31 08:48:14 EST 2005

On Mon, 31 Oct 2005 08:28:39 -0500, Pedro Sanchez <psanchez at nortel.com> wrote:
>I am the original poster of this thread. First of all, thank you for the 
>different responses. However I still don't have a clear answer (other than 
>what I'm thinking of doing is wrong, which is OK, it can be fixed).
>I'm working on a Twisted/Nevow application, and yes, I'm new to both and I 
>can tell that if learning Twisted has a step curve, doing it in conjuction 
>with Nevow is really almost vertical. But, nevertheless, my application is 
>working well these days.
>The motivation for my question is the following: I added the 
>guard/credentials stuff to my application. The issue is that in order to 
>accept a login my application requires several things to happen before 
>access can be granted. I therefore implemented my own AuthChecker() which 
>fires several deferreds to acomplish its task. This is working well, 
>including the use of deferToThread() in several places.
>But, the behavior of the authentication wrapper is synchronous. That is, 
>access to the system cannot be granted until all deferred operations have 
>happended. In this wrapper I have something like:
>     def requestAvatar(self, avatarId, mind, *interfaces):
>         for iface in interfaces:
>             if iface is inevow.IResource:
>                 if avatarId is checkers.ANONYMOUS:
>                     resc = login.LoginPage()
>                     resc.realm = self
>                     return (inevow.IResource, resc, noLogout)
>                 else:
>                     resc = home.RootPage()
>                     resc.realm = self
>                     return (inevow.IResource, resc,
>                             self.createLogout(avatarId, mind))
>             else:
>                 raise NotImplementedError("Not supported.")
>So let me rephrase my original question. Given that the real user 
>authentication is happening in my AuthChecker() implemented somewhere else, 
>with deferreds and what not, how can I force this requestAvatar() thing to 
>wait until the whole login process has finished? My idea was to force a 
>synchronous behavior on AuthChecker() forcing this way the login process to 
>wait for it to finish.
>While writting this I realize that I may be missunderstanding how the 
>credentials stuff works ... who knows.
>Any suggestions are appreciated,

Cred supports Deferreds, so there's no need to make your requestAvatar or requestAvatarId methods synchronous.  They can simply return Deferreds.  If you have several Deferreds going in requestAvatarId, you may want to use a DeferredList, which behaves very similarly to a Deferred, but takes several other Deferreds as input and only fires when all those Deferreds have fired.

requestAvatar won't be called until the Deferred you return from requestAvatarId fires.


More information about the Twisted-Python mailing list