#! usr/bin/env python
#encoding=utf8
from dreque import Dreque, DrequeWorker
import time
a=Dreque("127.0.0.1", db=None)
a=Dreque(("127.0.0.1",6379),db=10)
a.redis.flushdb()
def some_job(argument):
time.sleep(1)
print argument
#for i in xrange(100):
# a.enqueue("queue", some_job, argument=i)
def fun():
print "haha"
def calc(a,b,c,f):
print a
print b
print c
a.enqueue("queue", calc,"1","2","3",fun)
print "done!"
worker = DrequeWorker(["queue"],("127.0.0.1",6379),db=10)
worker.work()
它抛出如下的异常:
Traceback (most recent call last):
File "/home/mlzboy/bijia/dreque_utf8/mytest.py", line 23, in <module>
a.enqueue("queue", calc,"1","2","3",fun)
File "/home/mlzboy/bijia/dreque_utf8/dreque/base.py", line 96, in enqueue
self.push(queue, dict(func=func, args=args, kwargs=kwargs, retries_left=max_retries), delay=delay)
File "/home/mlzboy/bijia/dreque_utf8/dreque/base.py", line 46, in push
self.redis.lpush(self._queue_key(queue), self.encode(item))
File "/home/mlzboy/bijia/dreque_utf8/dreque/base.py", line 130, in encode
return self.serializer.dumps(value)
File "/home/mlzboy/bijia/dreque_utf8/dreque/serializer.py", line 71, in dumps
st = json.dumps(*args, **kwargs)
File "/usr/lib/python2.6/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
for chunk in self._iterencode_dict(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 275, in _iterencode_dict
for chunk in self._iterencode(value, markers):
File "/usr/lib/python2.6/json/encoder.py", line 306, in _iterencode
for chunk in self._iterencode_list(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 204, in _iterencode_list
for chunk in self._iterencode(value, markers):
File "/usr/lib/python2.6/json/encoder.py", line 317, in _iterencode
for chunk in self._iterencode_default(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 323, in _iterencode_default
newobj = self.default(o)
File "/home/mlzboy/bijia/dreque_utf8/dreque/serializer.py", line 42, in default
return super(JSONEncoder, self).default(o)
File "/usr/lib/python2.6/json/encoder.py", line 344, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function fun at 0x8f270d4> is not JSON serializable
从这里我们可以看出,由于在对fun函数尝试进行序列化时出现了问题,dreque是以json序列化的试将任务扔到消息队列中储存传递的,由于fun方法是一个函数,应该是无法将序列化储存而出现问题。
这也解开了我之前的一个疑问,如何得用redis来实现跨语言的存取,比如使用dreque存到redis中,使用C#的redis客户端来处理数据的功能,在理论上应该是可以实现的。
不过这里又引出一个问题,为什么函数不能序列化传输和储存呢,之前了解的串行反串行也都是数据或是状态的持久化,求高人指点
#encoding=utf8
from dreque import Dreque, DrequeWorker
import time
a=Dreque("127.0.0.1", db=None)
a=Dreque(("127.0.0.1",6379),db=10)
a.redis.flushdb()
def some_job(argument):
time.sleep(1)
print argument
#for i in xrange(100):
# a.enqueue("queue", some_job, argument=i)
def fun():
print "haha"
def calc(a,b,c,f):
print a
print b
print c
a.enqueue("queue", calc,"1","2","3",fun)
print "done!"
worker = DrequeWorker(["queue"],("127.0.0.1",6379),db=10)
worker.work()
它抛出如下的异常:
Traceback (most recent call last):
File "/home/mlzboy/bijia/dreque_utf8/mytest.py", line 23, in <module>
a.enqueue("queue", calc,"1","2","3",fun)
File "/home/mlzboy/bijia/dreque_utf8/dreque/base.py", line 96, in enqueue
self.push(queue, dict(func=func, args=args, kwargs=kwargs, retries_left=max_retries), delay=delay)
File "/home/mlzboy/bijia/dreque_utf8/dreque/base.py", line 46, in push
self.redis.lpush(self._queue_key(queue), self.encode(item))
File "/home/mlzboy/bijia/dreque_utf8/dreque/base.py", line 130, in encode
return self.serializer.dumps(value)
File "/home/mlzboy/bijia/dreque_utf8/dreque/serializer.py", line 71, in dumps
st = json.dumps(*args, **kwargs)
File "/usr/lib/python2.6/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
chunks = list(self.iterencode(o))
File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
for chunk in self._iterencode_dict(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 275, in _iterencode_dict
for chunk in self._iterencode(value, markers):
File "/usr/lib/python2.6/json/encoder.py", line 306, in _iterencode
for chunk in self._iterencode_list(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 204, in _iterencode_list
for chunk in self._iterencode(value, markers):
File "/usr/lib/python2.6/json/encoder.py", line 317, in _iterencode
for chunk in self._iterencode_default(o, markers):
File "/usr/lib/python2.6/json/encoder.py", line 323, in _iterencode_default
newobj = self.default(o)
File "/home/mlzboy/bijia/dreque_utf8/dreque/serializer.py", line 42, in default
return super(JSONEncoder, self).default(o)
File "/usr/lib/python2.6/json/encoder.py", line 344, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function fun at 0x8f270d4> is not JSON serializable
从这里我们可以看出,由于在对fun函数尝试进行序列化时出现了问题,dreque是以json序列化的试将任务扔到消息队列中储存传递的,由于fun方法是一个函数,应该是无法将序列化储存而出现问题。
这也解开了我之前的一个疑问,如何得用redis来实现跨语言的存取,比如使用dreque存到redis中,使用C#的redis客户端来处理数据的功能,在理论上应该是可以实现的。
不过这里又引出一个问题,为什么函数不能序列化传输和储存呢,之前了解的串行反串行也都是数据或是状态的持久化,求高人指点
![](http://img.zemanta.com/pixy.gif?x-id=cb6bbb29-e298-8d30-9e7f-3eef6d15dbb1)