Created
February 22, 2013 16:59
-
-
Save vi4m/5014902 to your computer and use it in GitHub Desktop.
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
import datetime | |
results = {} | |
from rq import Queue, use_connection | |
class InProgressException(Exception): | |
pass | |
def test(*args, **kwargs): | |
pass | |
def runable(report_name, params): | |
def _runable_(function): | |
def __runable__(*args, **kwargs): | |
frozen_params = frozenset(params.items()) | |
key = frozenset(dict( | |
report_name=report_name, params=frozen_params).items()) | |
report = results.get(key) | |
if not report: | |
use_connection() | |
q = Queue() | |
job = q.enqueue(test, params) | |
results[key] = dict( | |
timestamp=datetime.datetime.now(), | |
job=job, | |
) | |
raise InProgressException() | |
elif report['job'].status == 'queued': | |
raise InProgressException() | |
else: | |
return results[key]['job'].result | |
function(*args, **kwargs) | |
return __runable__ | |
return _runable_ | |
class MyReport(object): | |
@runable(report_name='myreport', params={'Date of start': 'start', | |
'Date of end': 'end'}) | |
def get_data(self, start, end): | |
return start + end | |
def run_report(self): | |
print self.get_data(start=10, end=20) | |
try: | |
MyReport().run_report() | |
except InProgressException: | |
print "Please wait" | |
MyReport().run_report() | |
MyReport().run_report() | |
MyReport().run_report() | |
MyReport().run_report() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment