Merge branch 'selwin-async-2'

This fixes #114.
This commit is contained in:
Vincent Driessen 2012-08-23 09:51:07 +02:00
commit f06ef9c975
3 changed files with 20 additions and 5 deletions

View File

@ -292,7 +292,8 @@ class Job(object):
def perform(self): # noqa
"""Invokes the job function with the job arguments.
"""
return self.func(*self.args, **self.kwargs)
self._result = self.func(*self.args, **self.kwargs)
return self._result
# Representation

View File

@ -41,12 +41,14 @@ class Queue(object):
name = queue_key[len(prefix):]
return cls(name, connection=connection)
def __init__(self, name='default', default_timeout=None, connection=None):
def __init__(self, name='default', default_timeout=None, connection=None,
async=True):
self.connection = resolve_connection(connection)
prefix = self.redis_queue_namespace_prefix
self.name = name
self._key = '%s%s' % (prefix, name)
self._default_timeout = default_timeout
self._async = async
@property
def key(self):
@ -158,6 +160,8 @@ class Queue(object):
If the `set_meta_data` argument is `True` (default), it will update
the properties `origin` and `enqueued_at`.
If Queue is instantiated with async=False, job is executed immediately.
"""
if set_meta_data:
job.origin = self.name
@ -168,8 +172,12 @@ class Queue(object):
else:
job.timeout = 180 # default
job.save()
self.push_job_id(job.id)
if self._async:
job.save()
self.push_job_id(job.id)
else:
job.perform()
job.save()
return job
def pop_job_id(self):

View File

@ -1,5 +1,5 @@
from tests import RQTestCase
from tests.fixtures import Calculator, say_hello, div_by_zero
from tests.fixtures import Calculator, div_by_zero, say_hello, some_calculation
from rq import Queue, get_failed_queue
from rq.job import Job
from rq.exceptions import InvalidJobOperationError
@ -260,3 +260,9 @@ class TestFailedQueue(RQTestCase):
self.assertEqual(job.result_ttl, 10)
job_from_queue = Job.fetch(job.id, connection=self.testconn)
self.assertEqual(int(job_from_queue.result_ttl), 10)
def test_async_false(self):
"""Executes a job immediately if async=False."""
q = Queue(async=False)
job = q.enqueue(some_calculation, args=(2, 3))
self.assertEqual(job.return_value, 6)