-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom serializers #22
base: master
Are you sure you want to change the base?
Changes from all commits
5045c4b
6d6e04c
5debfca
e9465f7
7c6e417
540dd94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import datetime | ||
import json | ||
import redis | ||
import time | ||
|
||
|
@@ -12,7 +11,7 @@ class Task(object): | |
def __init__(self, tiger, func=None, args=None, kwargs=None, queue=None, | ||
hard_timeout=None, unique=None, lock=None, lock_key=None, | ||
retry=None, retry_on=None, retry_method=None, | ||
_data=None, _state=None, _ts=None, _executions=None): | ||
_data=None, _state=None, _ts=None, _executions=None, deserialize=json.loads, serialize=json.dumps): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove these. |
||
""" | ||
Queues a task. See README.rst for an explanation of the options. | ||
""" | ||
|
@@ -269,7 +268,7 @@ def delay(self, when=None): | |
|
||
# When using ALWAYS_EAGER, make sure we have serialized the task to | ||
# ensure there are no serialization errors. | ||
serialized_task = json.dumps(self._data) | ||
serialized_task = self.tiger._serialize_data(self._data) | ||
|
||
if tiger.config['ALWAYS_EAGER'] and state == QUEUED: | ||
return self.execute() | ||
|
@@ -330,8 +329,8 @@ def from_id(self, tiger, queue, state, task_id, load_executions=0): | |
serialized_executions = [] | ||
# XXX: No timestamp for now | ||
if serialized_data: | ||
data = json.loads(serialized_data) | ||
executions = [json.loads(e) for e in serialized_executions if e] | ||
data = tiger._deserialize_data(serialized_data) | ||
executions = [tiger._deserialize_data(e) for e in serialized_executions if e] | ||
return Task(tiger, queue=queue, _data=data, _state=state, | ||
_executions=executions) | ||
else: | ||
|
@@ -369,8 +368,8 @@ def tasks_from_queue(self, tiger, queue, state, skip=0, limit=1000, | |
results = pipeline.execute() | ||
|
||
for serialized_data, serialized_executions, ts in zip(results[0], results[1:], tss): | ||
data = json.loads(serialized_data) | ||
executions = [json.loads(e) for e in serialized_executions if e] | ||
data = tiger._deserialize_data(serialized_data) | ||
executions = [tiger._deserialize_data.loads(e) for e in serialized_executions if e] | ||
|
||
task = Task(tiger, queue=queue, _data=data, _state=state, | ||
_ts=ts, _executions=executions) | ||
|
@@ -379,7 +378,7 @@ def tasks_from_queue(self, tiger, queue, state, skip=0, limit=1000, | |
else: | ||
data = tiger.connection.mget([tiger._key('task', item[0]) for item in items]) | ||
for serialized_data, ts in zip(data, tss): | ||
data = json.loads(serialized_data) | ||
data = tiger._deserialize_data(serialized_data) | ||
task = Task(tiger, queue=queue, _data=data, _state=state, | ||
_ts=ts) | ||
tasks.append(task) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import pickle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I preferably don't want to mention |
||
import datetime | ||
import json | ||
from multiprocessing import Pool | ||
|
@@ -32,7 +33,7 @@ def _ensure_queue(typ, data): | |
for name, n in data.items(): | ||
task_ids = self.conn.zrange('t:%s:%s' % (typ, name), 0, -1) | ||
self.assertEqual(len(task_ids), n) | ||
ret[name] = [json.loads(self.conn.get('t:task:%s' % task_id)) | ||
ret[name] = [self.tiger._deserialize_data(self.conn.get('t:task:%s' % task_id)) | ||
for task_id in task_ids] | ||
self.assertEqual(list(task['id'] for task in ret[name]), | ||
task_ids) | ||
|
@@ -46,6 +47,22 @@ def _ensure_queue(typ, data): | |
'scheduled': _ensure_queue('scheduled', scheduled), | ||
} | ||
|
||
|
||
class CustomSerializerTestCase(BaseTestCase): | ||
def setUp(self): | ||
self.tiger = get_tiger(SERIALIZER=lambda x: pickle.dumps(x).decode('latin1'), DESERIALIZER=lambda x: pickle.loads(x.encode('latin1'))) | ||
self.conn = self.tiger.connection | ||
self.conn.flushdb() | ||
|
||
def test_simple_task(self): | ||
self.tiger.delay(simple_task, queue='custom_ser') | ||
queues = self._ensure_queues(queued={'custom_ser': 1}) | ||
task = queues['queued']['custom_ser'][0] | ||
Worker(self.tiger).run(once=True) | ||
self._ensure_queues(queued={'custom_ser': 0}) | ||
self.assertFalse(self.conn.exists('t:task:%s' % task['id'])) | ||
|
||
|
||
class TestCase(BaseTestCase): | ||
""" | ||
TaskTiger main test cases. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to have a quick comment just like the other settings.