Followers 0

# Python Twisted question: need help on deferred

## 4 posts in this topic

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, service

import protocols, services

udp_port = 10000

dbuser = 'testuser'

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 DatagramProtocol

class MulticastGameProtocol(DatagramProtocol):

self.service = service

def startProtocol(self):
self.transport.setTTL(5)

if '.' not in datagram:
return 'Invalid command format.'

celcommand_name, arguments = datagram.split('.', 1)

command_result = self.doCommand(celcommand_name, arguments)

if command_result is not None:

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))



[/spoiler]

The services.py

[spoiler]

from twisted.application import service
from twisted.python import log
from twisted.internet import defer

from twistar.dbobject import DBObject

class 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 twistar.registry import Registry

Registry.DBPOOL = adbapi.ConnectionPool('MySQLdb', user=dbuser, passwd=dbpass, db='test')
self.user = Users()

if query_result is None:
else:
print '%s logged in' % (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.

Edited by Sky Warden
0

##### Share on other sites

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.

0

##### Share on other sites

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.

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?

Edited by Sky Warden
0

##### Share on other sites

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.

0

##### Share on other sites

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.

0

## Create an account

Register a new account