• Advertisement
Sign in to follow this  

Need Help With Twisted Deferred

This topic is 1651 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Terribly sorry. It was just a small mistake. I've fixed it. I forgot that I should have returned the deferred object. Sorry everyone.

 

[spoiler]

Hi everyone. Now I'm studying network programming with Python Twisted. I'm pretty familiar with deferred, and I understand asynchronous programming a little bit, but there's still this one issue I still can't solve. Let me hand you some code to understand my situation first.

from twisted.protocols import amp
from twisted.python import log
from commands import Login

class LoginAMPServer(amp.AMP):

    def login(self, username, password):
        #This is just test log, so please spare me.
        log.msg('Got username %s and password %s' % (username, password))

        return {'username': username, 'password': username}

    Login.responder(login)

When the client use callRemote to the login command, it will get the result {'username': username, 'password': username}. Everything works fine. My problem is, this login function will need to fetch some stuff from the database, which operation uses deferred objects, and the login function needs to get the result from that query, return it and yeah... I'm expecting something like this:

from twisted.protocols import amp
from twisted.python import log
from twisted.internet import defer
from commands import Login

class LoginAMPServer(amp.AMP):

    def test_return(self, result):
        log.msg('FIRED %s!' % repr(result))
        username, password = result
        return {'username': username, 'password': username}

    def test_callback(self, username, password):
        return [username, password]

    def login(self, username, password):
        log.msg('Got username %s and password %s' % (username, password))

        deferred = defer.Deferred()
        deferred.addCallback(self.test_callback)
               
        return deferred.callback([username, password])
        #return {'username': username, 'password': username}


    Login.responder(login)

I'm wondering if I can catch the result caught in the last callback with the parent function, so it can return it as well. What I know is that callbacks are like a set of function calls which is started when a result is ready, and it passes its return value to the next callbacks, so I'm wondering if I can get the last result and return it with the login function. I apologize if my question isn't clear enough. I hope you people can get my question.

 

Thank you.

[/spoiler]

Edited by Sky Warden

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement