New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tornado.gen does not work with callbacks that have kwargs #351
Comments
accidentally closed the issue, reopened it |
actually, adding **kwargs to the callback() method does fix the problem, but does nothing to expose those keyword arguments. def callback(self, arg=None, **kwargs):
self.runner.set_result(self.key, arg) don't know the best way to expose them... |
Yeah, the trick is that we can only pass a single object through the yield expression. The only way to handle all the possible argument scenarios would be to pass a (args, kwargs) tuple, but that's awkward to work with. Does asyncmongo ever give both positional and keyword arguments at the same time? If not, we could automatically detect no args, one positional arg, or no positional args with keyword arguments (but then kwargs would come through as a dictionary and be indistinguishable from one positional arg that happened to be a dict). The most general solution is to have several variants of gen.Callback depending on the expected signature. |
I think asyncmongo always passes an 'error' kwarg, and the result is On Sep 7, 2011, at 12:54 AM, bdarnell
|
Returning (args, kwargs) is really awkward to work with. You don't want to have to do something like this on every call:
You can kind of make an adapter with the Callback/Wait interface:
That could be made a little less clunky with support in Callback (and a few standard adapters could be predefined), but it's still messy (and making the analogous change to Task risks name collisions):
Maybe returning (args, kwargs) and requiring postprocessing is the best solution (especially since the postprocessing can easily be put into a function and not entangled with the gen framework).
The case of a single positional argument is so common that it probably deserves to be handled specially, even if that means isinstance checks will be required for functions whose signatures vary. |
I'm playing around with tornado.gen, and it seems to croak on callbacks that are triggered with keyword arguments. Specifically, asyncmongo sends an "error" keyword argument to the callback you specify. The tornado.gen.Task class's callback() method isn't set up to handle this. Here is the relevant code:
the error: "TypeError: callback() got an unexpected keyword argument 'error'"
I'm hacking around trying to figure out a fix to this, but I still haven't wrapped my head around the gen.py code. I tried just adding a **kwargs argument to the callback() function, but that didn't work (the Runner loop timed out). this is similar to the hack I used to make this code work with swirl.
The text was updated successfully, but these errors were encountered: