Created
February 17, 2016 10:18
-
-
Save pablasso/8b976c6ecf3173337e23 to your computer and use it in GitHub Desktop.
Queue Runner Proof of Concept
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
# flake8: noqa | |
from datetime import datetime | |
import time | |
class QueueRunnerTimeoutException(Exception): | |
pass | |
class QueueRunnerService(object): | |
@classmethod | |
def run(cls, func, *args, **kwargs): | |
kwargs.update({'timeout': kwargs.get('timeout', 2.5)}) | |
try: | |
func(*args, **kwargs) | |
print('finished succesfully!') | |
except QueueRunnerTimeoutException as e: | |
print('catched QueueRunnerTimeoutException') | |
print(e.args) | |
args = e.args[0].get('args', []) | |
kwargs = e.args[0].get('kwargs', {}) | |
cls.run(func, *args, **kwargs) | |
class QueueRunnerServiceMixin(object): | |
seconds_elapsed = 0 | |
def timer_start(self, timeout=None): | |
self.start_date = datetime.now() | |
self.timeout = timeout | |
print('-start:seconds_elapsed:%s' % self.seconds_elapsed) | |
def timer_set_data(self, *args, **kwargs): | |
self.args = args | |
self.kwargs = kwargs | |
print('-set_data: args:%s kwargs:%s' % (self.args, self.kwargs)) | |
def timer_checkpoint(self): | |
self.seconds_elapsed = (datetime.now() - self.start_date).seconds | |
print('-checkpoint:seconds_elapsed:%s' % self.seconds_elapsed) | |
if self.timeout is not None: | |
if self.seconds_elapsed > self.timeout: | |
print('timeout reached') | |
raise QueueRunnerTimeoutException({'args': self.args, 'kwargs': self.kwargs}) | |
# items = [1,2,3,4,5,6,7,8,9,10] | |
# current_items = items[:5] | |
# current_items | |
# items = items[5:] | |
# items | |
# | |
# items = range(1, 10) | |
# while len(items): | |
# items = items[1:] | |
# print(items) | |
class SampleClient(QueueRunnerServiceMixin): | |
@classmethod | |
def runner(cls, a, b, items=[], **kwargs): | |
print('starting SampleClient:runner') | |
instance = cls() | |
instance.timer_start(timeout=kwargs.get('timeout')) | |
instance.timer_set_data(a, b, items=items) | |
instance.slow_method_a(a) | |
instance.slow_method_b(b) | |
items = items[1:] | |
instance.timer_set_data(a, b, items=items) | |
instance.timer_checkpoint() | |
def slow_method_a(self, param_a): | |
print('..starting slow_method_a') | |
time.sleep(1) | |
def slow_method_b(self, param_b): | |
print('..starting slow_method_b') | |
print('..items on slow_method_b %s' % self.kwargs) | |
time.sleep(len(self.kwargs.get('items'))) | |
QueueRunnerService.run(SampleClient.runner, 'a', 'b', items=range(0, 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment