Python redis pubsub: what happen to types when it gets published? -
pub.py
import redis import datetime import time def main(): redis_host = '10.235.13.29' r = redis.client.strictredis(host=redis_host, port=6379) while true: = datetime.datetime.now() print 'sending {0}'.format(now) print 'data type %s' % type(now) r.publish('clock', now) time.sleep(1) if __name__ == '__main__': main()
output:
sending 2014-10-08 13:10:58.338765 info type <type 'datetime.datetime'> sending 2014-10-08 13:10:59.368707 info type <type 'datetime.datetime'> sending 2014-10-08 13:11:00.378723 info type <type 'datetime.datetime'> sending 2014-10-08 13:11:01.398132 info type <type 'datetime.datetime'> sending 2014-10-08 13:11:02.419030 info type <type 'datetime.datetime'>
sub.py
import redis import threading import time import datetime def callback(): redis_host = '10.235.13.29' r = redis.client.strictredis(host=redis_host, port=6379) sub = r.pubsub() sub.subscribe('clock') while true: m in sub.listen(): #print m #'recieved: {0}'.format(m['data']) = datetime.datetime.now() print 'recieved: %s @ %s' % (m['data'], now) print 'data type %s' % type(m['data']) dur = 1 print 'it took %s receive' % dur def main(): t = threading.thread(target=callback) t.setdaemon(true) t.start() while true: print 'waiting' time.sleep(30) if __name__ == '__main__': main()
output:
{}: ./sub.py waiting recieved: 1 @ 2014-10-08 13:09:36.708088 info type <type 'long'> took 1 receive recieved: 2014-10-08 13:09:37.629664 @ 2014-10-08 13:09:37.630479 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:38.630661 @ 2014-10-08 13:09:38.631585 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:39.632663 @ 2014-10-08 13:09:39.633480 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:40.633662 @ 2014-10-08 13:09:40.634464 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:41.634665 @ 2014-10-08 13:09:41.635557 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:42.635662 @ 2014-10-08 13:09:42.636673 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:43.642665 @ 2014-10-08 13:09:43.643441 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:44.643663 @ 2014-10-08 13:09:44.644582 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:45.644667 @ 2014-10-08 13:09:45.673734 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:46.672918 @ 2014-10-08 13:09:46.673874 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:47.673913 @ 2014-10-08 13:09:47.675014 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:48.674920 @ 2014-10-08 13:09:48.675804 info type <type 'str'> took 1 receive recieved: 2014-10-08 13:09:49.675912 @ 2014-10-08 13:09:49.677346 info type <type 'str'>
the type changed datetime.datetime str possible preserve type because trying find duration cant subtracte datetime obj str?
see http://pymotw.com/2/pickle/
i think must serialize datetime object in published channel, deserialize when reading sub.
i noticed commented on solution :) http://stackoverflow.com/a/20400288/152016
edit:
you store epoch seconds in key , avoid serializing! (if publish datetimes).
python redis redis-py
No comments:
Post a Comment