yandex-tank/yandextank/plugins/bfg/zmq_reader.py

42 lines
1.0 KiB
Python
Raw Normal View History

2014-09-08 16:55:17 +00:00
import zmq
import logging
2014-09-10 12:17:28 +00:00
import json
import threading as th
2014-09-08 16:55:17 +00:00
class ZmqReader(object):
'''Read missiles from zmq'''
def __init__(self, queue):
self.queue = queue
self.quit = th.Event()
2014-09-08 16:55:17 +00:00
self.log = logging.getLogger(__name__)
context = zmq.Context()
2014-09-10 12:17:28 +00:00
self.socket = context.socket(zmq.PULL)
self.socket.connect(self.queue)
self.log.info("ZMQ: Waiting for missiles from '%s'" % self.queue)
2014-09-08 16:55:17 +00:00
def __iter__(self):
while True:
try:
if not self.socket.poll(500):
if self.quit.is_set():
raise StopIteration()
continue
data = tuple(self.socket.recv_json())
if len(data) != 3:
if data[0] == "stop":
raise StopIteration()
continue
yield(data)
except StopIteration:
raise
2014-09-08 16:55:17 +00:00
except Exception as e:
print(e)
def stop(self):
self.quit.set()