brianthelion

min_zmq_test.py

May 14th, 2011
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import zmq
  2. from multiprocessing import Process
  3. import time
  4.  
  5. SLEEPLEN = .033 # Attempts to approximate a vision system at 30fps
  6.  
  7. def src(loc) :
  8.    
  9.     context = zmq.Context()
  10.     socket = context.socket(zmq.PUB)
  11.     socket.setsockopt(zmq.HWM,1)
  12.     socket.bind(loc)
  13.     while True :
  14.         socket.send( repr(time.time()) ) # Use repr to keep as much precision as possible
  15.         time.sleep(SLEEPLEN)
  16.  
  17. def snk(locs) :
  18.    
  19.     context = zmq.Context()
  20.     socket = context.socket(zmq.SUB)
  21.     socket.setsockopt(zmq.HWM,1)
  22.     socket.setsockopt(zmq.SUBSCRIBE,'')
  23.     for l in locs : socket.connect(l)
  24.     while True :
  25.     print time.time() - float(socket.recv())
  26.     time.sleep(SLEEPLEN)
  27.  
  28. def main() :
  29.    
  30.     addrs = ['ipc:///dev/shm/10001','ipc:///dev/shm/10002','ipc:///dev/shm/10003','ipc:///dev/shm/10004']
  31. #    addrs = ['tcp://*:10001','tcp://*:10002','tcp://*:10003','tcp://*:10004']
  32.  
  33.     Process( target=snk, args=(addrs,) ).start()
  34.     time.sleep(1)
  35.     for a in addrs : Process( target=src,args=(a,) ).start()
  36.  
  37.  
  38. if __name__ == '__main__' : main()
  39. else : pass
Advertisement
Add Comment
Please, Sign In to add comment