Put Job in its own file.

main
Vincent Driessen 13 years ago
parent 1f64157c38
commit db5753b0d6

@ -0,0 +1,41 @@
from pickle import loads
from .exceptions import DequeueError
class Job(object):
"""A Job is just a convenient datastructure to pass around job (meta) data.
"""
__slots__ = ['func', 'args', 'kwargs', 'rv_key', 'origin']
@classmethod
def unpickle(cls, pickle_data):
"""Constructs a Job instance form the given pickle'd job tuple data."""
try:
job_tuple = loads(pickle_data)
return Job(job_tuple)
except (AttributeError, ValueError, IndexError):
raise DequeueError('Could not decode job tuple.')
def __init__(self, job_tuple, origin=None):
self.func, self.args, self.kwargs, self.rv_key = job_tuple
self.origin = origin
def perform(self):
"""Invokes the job function with the job arguments.
"""
return self.func(*self.args, **self.kwargs)
@property
def call_string(self):
"""Returns a string representation of the call, formatted as a regular
Python function invocation statement.
"""
arg_list = map(repr, self.args)
arg_list += map(lambda tup: '%s=%r' % (tup[0], tup[1]),
self.kwargs.items())
return '%s(%s)' % (self.func.__name__, ', '.join(arg_list))
def __str__(self):
return '<Job %s>' % self.call_string

@ -2,7 +2,8 @@ import uuid
from functools import total_ordering from functools import total_ordering
from pickle import loads, dumps from pickle import loads, dumps
from .proxy import conn from .proxy import conn
from .exceptions import DequeueError from .job import Job
class DelayedResult(object): class DelayedResult(object):
"""Proxy object that is returned as a result of `Queue.enqueue()` calls. """Proxy object that is returned as a result of `Queue.enqueue()` calls.
@ -37,43 +38,6 @@ class DelayedResult(object):
return self._rv return self._rv
class Job(object):
"""A Job is just a convenient datastructure to pass around job (meta) data.
"""
__slots__ = ['func', 'args', 'kwargs', 'rv_key', 'origin']
@classmethod
def unpickle(cls, pickle_data):
"""Constructs a Job instance form the given pickle'd job tuple data."""
try:
job_tuple = loads(pickle_data)
return Job(job_tuple)
except (AttributeError, ValueError, IndexError):
raise DequeueError('Could not decode job tuple.')
def __init__(self, job_tuple, origin=None):
self.func, self.args, self.kwargs, self.rv_key = job_tuple
self.origin = origin
def perform(self):
"""Invokes the job function with the job arguments.
"""
return self.func(*self.args, **self.kwargs)
@property
def call_string(self):
"""Returns a string representation of the call, formatted as a regular
Python function invocation statement.
"""
arg_list = map(repr, self.args)
arg_list += map(lambda tup: '%s=%r' % (tup[0], tup[1]),
self.kwargs.items())
return '%s(%s)' % (self.func.__name__, ', '.join(arg_list))
def __str__(self):
return '<Job %s>' % self.call_string
@total_ordering @total_ordering
class Queue(object): class Queue(object):
redis_queue_namespace_prefix = 'rq:queue:' redis_queue_namespace_prefix = 'rq:queue:'

Loading…
Cancel
Save