Jump to content

  • Log In with Google      Sign In   
  • Create Account


Python Twisted question: need help on deferred


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Herwin P   Members   -  Reputation: 629

Like
0Likes
Like

Posted 22 May 2013 - 08:06 PM

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

 

The protocols.py

 

Spoiler

 

The services.py

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. smile.png


Edited by Sky Warden, 25 May 2013 - 01:09 AM.


Sponsor:

#2 Steve_Segreto   Crossbones+   -  Reputation: 1475

Like
0Likes
Like

Posted 23 May 2013 - 01:50 AM

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.



#3 Herwin P   Members   -  Reputation: 629

Like
0Likes
Like

Posted 23 May 2013 - 02:43 AM

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?


Edited by Sky Warden, 23 May 2013 - 02:51 AM.


#4 Kylotan   Moderators   -  Reputation: 3329

Like
0Likes
Like

Posted 23 May 2013 - 05:18 AM

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.



#5 Herwin P   Members   -  Reputation: 629

Like
0Likes
Like

Posted 23 May 2013 - 06:50 AM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS