简体   繁体   中英

Returning (Passing Around) A Function Call in Python/Tornado?

So I'm creating the back end for a web-based game in python. Currently it works like this...

WebSocket Handler receives message...

WebSocket Handler calls message handler...

Message Handler calls Game class functions...

Game class calls other classes to update information.

This is highly coupled, and probably should be in model-view-controller format. So I'm finally going to change that.

Basically I want it to work like this.

Controller has open a WebSocket Handler.

WebSocket Handler returns to Controller a (1?).

Controller uses (1?) calls Message Handler.

Message Handler returns to Controller a (2?).

Contoller uses (2?) to call Model.

Model sends updates to appropriate places.

So there's two problems here.

  1. First of all, when I'm getting a message in the WebSocket Handler, it is an instance of Tornado's WebSocketHandler, and I'm not sure how I can return anything to the Controller. Is the case simply that this is not possible? Do I have to keep a small amount of coupling between the WebSocket Handler and the Message Handler? I know I could always call a function in Controller, but that doesn't seem like an actual fix, just more function calls.

  2. Is there a way to pass a function call around in python, keeping track of it's parameters while doing so? That would be the optimal way to go about doing this, but I don't think it's implemented in the python language. Otherwise, I feel like the best way to do it would be to return a dictionary with a field for the function name to be called, and fields for the parameters. This of course is a lot more code. If it can be avoided, I'd like to, but I'm not sure the direction in which to take this.

Thanks for the tips guys, this is a big refactoring and I'm really nervous about where to start.

For the second part of your question, I believe you want to use partial functions.

Check out: http://docs.python.org/2/library/functools.html

Basically you would go:

from functools import partial

function_call(partial(future_function_call, future_argument))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM