Python redis 发布订阅 : what happen to types when it gets published?

标签 python redis redis-py

发布.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:
            now = datetime.datetime.now()
            print 'Sending {0}'.format(now)
            print 'data type is %s' % type(now)
            r.publish('clock', now)
            time.sleep(1)

if __name__ == '__main__':
  main()

输出:

Sending 2014-10-08 13:10:58.338765
data type is <type 'datetime.datetime'>
Sending 2014-10-08 13:10:59.368707
data type is <type 'datetime.datetime'>
Sending 2014-10-08 13:11:00.378723
data type is <type 'datetime.datetime'>
Sending 2014-10-08 13:11:01.398132
data type is <type 'datetime.datetime'>
Sending 2014-10-08 13:11:02.419030
data type is <type 'datetime.datetime'>

子.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:
        for m in sub.listen():
            #print m #'Recieved: {0}'.format(m['data'])
            now = datetime.datetime.now()
            print 'Recieved: %s at %s' % (m['data'], now)
            print 'Data type is %s' % type(m['data'])
            dur = 1
            print 'It took %s to 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()

输出:

{}: ./sub.py
Waiting
Recieved: 1 at 2014-10-08 13:09:36.708088
Data type is <type 'long'>
It took 1 to receive
Recieved: 2014-10-08 13:09:37.629664 at 2014-10-08 13:09:37.630479
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:38.630661 at 2014-10-08 13:09:38.631585
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:39.632663 at 2014-10-08 13:09:39.633480
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:40.633662 at 2014-10-08 13:09:40.634464
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:41.634665 at 2014-10-08 13:09:41.635557
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:42.635662 at 2014-10-08 13:09:42.636673
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:43.642665 at 2014-10-08 13:09:43.643441
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:44.643663 at 2014-10-08 13:09:44.644582
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:45.644667 at 2014-10-08 13:09:45.673734
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:46.672918 at 2014-10-08 13:09:46.673874
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:47.673913 at 2014-10-08 13:09:47.675014
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:48.674920 at 2014-10-08 13:09:48.675804
Data type is <type 'str'>
It took 1 to receive
Recieved: 2014-10-08 13:09:49.675912 at 2014-10-08 13:09:49.677346
Data type is <type 'str'>

类型由 datetime.datetime 变为 str
是否可以保留类型,因为我试图找到无法将 datetime obj 减去 str 的持续时间?

最佳答案

参见 http://pymotw.com/2/pickle/

我认为您必须在已发布 channel 中序列化日期时间对象,然后在读取子 channel 时反序列化。

我注意到您已经对解决方案发表了评论:) https://stackoverflow.com/a/20400288/152016


编辑:

您可以在 key 中存储纪元秒并避免序列化! (如果您只发布日期时间)。

关于Python redis 发布订阅 : what happen to types when it gets published?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262633/

相关文章:

python - 在使用 Redis 发布/订阅的 Python 服务中调试内存泄漏

python - 使用 urlwatch 观看页面的一部分

python - 如何使用变量字符串数据在Python中动态创建列表字典

python - 如何在 TwitchIO 中发送不是对命令的响应的消息?

laravel-5 - Laravel 5.3 和 Redis (predis) - 自动增量散列和删除散列 `row`

python - 使用 Flask 获取 int 列表并将这些值作为端点返回

python - Flask 应用程序无法在浏览器中加载

python - 将 numpy 数组转换为纯 python 整数以避免整数溢出

javascript - 如何使用 Django 显示日志

node.js - Node redis,变量在客户端之间共享?