def create_publisher(message_num, thread_num, hostname, port): global g_publish_finish_time published = list() def _on_publish(mosq, obj, mid): published.append(mid) #_logger.debug('got publish ack %d' % mid) def _on_disconnect(mosq, obj, rc): global g_lost_connection_count if rc != 0: _logger.error( 'publisher lost connection to server, will reconnect') g_lost_connection_count += 1 create_publisher(message_num, thread_num, hostname, port) pub_id = "publisher-%s" % (socket.gethostname()) _logger.debug("publisher id is (%s)" % pub_id) client = GossipClient(client_id=pub_id) client.on_publish = _on_publish client.on_disconnect = _on_disconnect client.connect(hostname, port) for i in xrange(message_num): for j in xrange(thread_num): now = time.time() client.publish(topic="%s-%d" % (socket.gethostname(), j + 1), payload=str(now), qos=1) count = i * thread_num + j if count % 100 == 0: _logger.debug("published %d messages" % count) ret = client.loop(10.0) # Mosquitto won't work without this sleep if ret != 0: _logger.fatal("publish loop returned %d, will retry" % ret) return create_publisher(message_num, thread_num, hostname, port) while len(published) < message_num * thread_num: _logger.debug("published %d" % len(published)) ret = client.loop() if ret != 0: _logger.fatal("publisher's connection is lost, rc=%d" % rc) sys.exit(0) g_publish_finish_time = time.time() _logger.info("all messages published")
def __init__(self, worker_id, thread_num, hostname, port): self.client_id = "%s_test_%d" % (socket.gethostname(), worker_id) self.worker_id = worker_id self.thread_num = thread_num self.hostname = hostname self.port = port self.client = GossipClient(client_id=self.client_id, clean_session=True) self.client.on_connect = self.on_connect self.client.on_disconnect = self.on_disconnect self.client.on_publish = self.on_publish self.client.on_subscribe = self.on_subscribe self.client.on_message = self.on_message self.client.on_unsubscribe = self.on_unsubscribe self.subscribe_done = False