Created
April 3, 2013 02:59
-
-
Save PaulMcMillan/5298107 to your computer and use it in GitHub Desktop.
Chaining Celery Positional Args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use this wrapper with functions in chains that return a tuple. The | |
# next function in the chain will get called with that the contents of | |
# tuple as (first) positional args, rather than just as just the first | |
# arg. Note that both the sending and receiving function must have | |
# this wrapper, which goes between the @task decorator and the | |
# function definition. This wrapper should not otherwise interfere | |
# when these conditions are not met. | |
class UnwrapMe(object): | |
def __init__(self, contents): | |
self.contents = contents | |
def __call__(self): | |
return self.contents | |
def wrap_for_chain(f): | |
""" Too much deep magic. """ | |
@functools.wraps(f) | |
def _wrapper(*args, **kwargs): | |
if type(args[0]) == UnwrapMe: | |
args = list(args[0]()) + list(args[1:]) | |
result = f(*args, **kwargs) | |
if type(result) == tuple and current_task.request.callbacks: | |
return UnwrapMe(result) | |
else: | |
return result | |
return _wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment