Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created August 22, 2014 14:43
Show Gist options
  • Save igniteflow/220b44ee7393954dd923 to your computer and use it in GitHub Desktop.
Save igniteflow/220b44ee7393954dd923 to your computer and use it in GitHub Desktop.
Sequential method callbacks. In Appengine this would allow a deferred task to defer another task, and this task would defer another etc. so that tasks are completed sequentially
#!/usr/bin/env python
from time import sleep
def simulate_defer(_callable, *args, **kwargs):
"""mocking Appengine's deferred.defer"""
sleep(2)
_callable(*args, **kwargs)
def a(callables):
print('a')
callback(callables)
def b(callables):
print('b')
callback(callables)
def c(callables):
print('c')
callback(callables)
def callback(callables):
"""
:param callables: a list of callables
:return:
"""
if callables:
_callable = callables.pop(0)
simulate_defer(_callable, callables)
if __name__ == "__main__":
# run a, then b and then c in that order. Useful when functions are run synchronously for example
# if they are being delegated to a task queue which has a limited timeout per task
a([b, c])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment