Created
August 22, 2014 14:43
-
-
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
This file contains hidden or 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
| #!/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