2014-10-08 2 views
1

pub.pyPython redis pubsub : 게시 될 때 유형이 어떻게됩니까?

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() 

OUTPUT :

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'> 

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: 
     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() 

가 OUTPUT :

{}: ./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 to
str에 내림차순 datetime obj를 찾을 수 없기 때문에 유형을 유지할 수 있습니까?

답변

2

난 당신이 하위를 읽을 때 다음 역 직렬화, 출판 채널에서 날짜 객체를 직렬화 생각합니다 http://pymotw.com/2/pickle/

를 참조하십시오.

당신은 키의 시대 초를 저장하고 직렬화 피할 수 :

난 당신이 솔루션에 이미 :) https://stackoverflow.com/a/20400288/152016


편집 댓글을 달았 발견! (당신이 오직 datetimes를 공개한다면).

+0

직렬화가 작동했습니다. 고맙습니다! – ealeon