   Python Twisted question: need help on deferred
Hi everyone. I'm writing a Twisted based daemon for a simple text-based multiplayer game. The game logic goes like this. The client sends a string in a particular format (command.arguments) to the server. The server receives that string, split it, and run the command along with the arguments. I already made it working. One of the command called has to get data from database. I'm using Twistar to make things fancy.  The problem is, I need to return the result of the deferred made by the query. I know it sounds so newbie, but this is my first Twisted program, and I'm still learning. Well, I think the scripts will make things clearer: The server.py (don't worry, it's inside a code tag):[spoiler]from twisted.application import internet, serviceimport protocols, servicesudp_port = 10000multicast_addr = '234.0.0.1'dbuser = 'testuser'dbpass = 'password'main_service = service.MultiService()game_service = services.GameService(dbuser, dbpass)game_service.setServiceParent(main_service)udp_service = internet.MulticastServer(udp_port, protocols.MulticastGameProtocol(multicast_addr, game_service), listenMultiple=True)udp_service.setServiceParent(main_service)application = service.Application('Simple Thing')main_service.setServiceParent(application) [/spoiler] The protocols.py [spoiler] from twisted.internet.protocol import DatagramProtocolclass MulticastGameProtocol(DatagramProtocol):    def __init__(self, multicast_addr, service):   self.multicast_addr = multicast_addr   self.service = service    def startProtocol(self):   self.transport.setTTL(5)   self.transport.joinGroup(self.multicast_addr)  def datagramReceived(self, datagram, address):   print 'Datagram %s received from %s' % (repr(datagram), repr(address))   if '.' not in datagram:  return 'Invalid command format.'   celcommand_name, arguments = datagram.split('.', 1)self.celcommandRequestReceived(celcommand_name, arguments, address)  def celcommandRequestReceived(self, celcommand_name, arguments, address):   command_result = self.doCommand(celcommand_name, arguments) if command_result is not None:  self.transport.write(command_result, address) def doCommand(self, celcommand_name, arguments):   submitted_command = getattr(self, 'celcommand_%s' % (celcommand_name), None) if submitted_command is None:  return 'Invalid command.'try:  #return 'COMMAND SUCCESS!!!'  return repr(submitted_command(arguments))   except:  #return 'Command failed.'  return repr(submitted_command(arguments)) def celcommand_login(self, arguments):   username, password = arguments.split('.', 1)   return self.service.login(username, password)[/spoiler] The services.py[spoiler] from twisted.application import servicefrom twisted.python import logfrom twisted.internet import deferfrom twistar.dbobject import DBObjectclass Users(DBObject):    pass    class GameService(service.Service):    def __init__(self, dbuser, dbpass):   self.dbuser = dbuser   self.dbpass = dbpass  def startService(self):   service.Service.startService(self) from twisted.enterprise import adbapi   from twistar.registry import Registry   Registry.DBPOOL = adbapi.ConnectionPool('MySQLdb', user=dbuser, passwd=dbpass, db='test')   self.user = Users() log.msg('Made connection to database.')  def login(self, username, password):   self.loginUser = self.user.find(where=['username="%s" AND password_hash="%s"' % (username, password)], limit=1)   self.loginUser.addCallback(self.checkLogin)   return self.loginUser  def checkLogin(self, query_result):   if query_result is None:  return 'Login failed!'   else:  print '%s logged in' % (query_result.username)  return query_result.username [/spoiler] I'm sure the error is in the login function. I think it's like saying "Wait. My result isn't here yet" by returning a deferred, but it will be the deferred which is sent to the client. Well, there's no complicated logic yet. I started writing this just two days ago. All I want is to send the query_result.username to the client. The query is fine. I can print that thingy on the server. What I want : the login() function returns the result of the query (a list, string or whatever) so I can send it to the client.What I get : the login() function returns a deferred object. The client is just a simple client which sends 'login.Sky Warden.123'. The arguments will be separated by the '.' as well. I think that's all. Maybe it's just me who don't fully understand deferreds, or it's just a logic hole. Or maybe both. XD Thank you everyone. 
Did you ever figure this out? I tried to follow the logic, but it looks like when you add checkLogin as a callback you don't have an opportunity to specify what the values of the parameters are. Then when checkLogin does get called back, its query_result parameter is not initialized and is set to None. Maybe I missed some subtlety of Python, but I don't see you sending parameter values into checkLogin.;Did you ever figure this out? I tried to follow the logic, but it looks like when you add checkLogin as a callback you don't have an opportunity to specify what the values of the parameters are. Then when checkLogin does get called back, its query_result parameter is not initialized and is set to None. Maybe I missed some subtlety of Python, but I don't see you sending parameter values into checkLogin.Thanks for the reply, Steve. As far as I know, the result (in this case, the query result) will be passed to the callback (checkLogin) as the first argument. I think it is correct considering that query_result can be printed out on the server. I think the error is in the login() function. It has to return something. It currently returns a deferred, but somehow, when the deferred fires and we get the result, the login() doesn't seem returning query_result. By the way, do people use this kind of command-line thingy in multiplayer game?;What exactly is the problem? You've said there's a problem and an error, but I don't see you specifying exactly what you can't do, or what error is occurring. If the issue is that checkLogin goes some data but you don't know what to do with it, then there are various approaches. Either have it store a value locally and poll for it from elsewhere, or register a callback which checkLogin can call for you when itself is called, passing in the information it's received.;What exactly is the problem? You've said there's a problem and an error, but I don't see you specifying exactly what you can't do, or what error is occurring. If the issue is that checkLogin goes some data but you don't know what to do with it, then there are various approaches. Either have it store a value locally and poll for it from elsewhere, or register a callback which checkLogin can call for you when itself is called, passing in the information it's received.Eh, I haven't specified it? Sorry then.What I want is the login() to return the result of the query, so the calling function can get it. Like query_result.id. Since login() needs to run query then it's an asynchronous function. That means it returns a deferred. Then it does the query, the deferred fires and then checkLogin runs.The function login() is called by another function. Simply I want that function (which calls login()) gets the result of the query (string), not a deferred object.