def test_keep_alive(): # 创建生产者 p = Producer(ip=g_ip, user=g_user, password=g_password) p.producer_declare() p.create_exchange(g_exchange, "topic") # 创建消费者 c = Consumer(ip=g_ip, user=g_user, password=g_password) c.start_consumer(g_exchange, "test1", "test1") time.sleep(5) # 等5秒让队列准备就绪 # 保持不发送任何消息 log.info("[test_keep_alive] start sending nothing test ...") secs = 0 while secs < g_test_secs: time.sleep(5) secs += 5 try: # 发送一次消息检查连接可用性 log.info("[test_keep_alive] test connection alive???") p.publish(g_exchange, "docx2pdf", '{"msg":"this is a test!"}') log.info("[test_keep_alive] connection alive!!!") log.info("[test_keep_alive] start sending msg test ...") secs = 0 while secs < g_test_secs: time.sleep(1) p.publish(g_exchange, "test1", '{"msg":"this is a test!"}') secs += 1 except Exception as err: log.error("[test_keep_alive] error: " + str(err)) log.error("exit [test_keep_alive]") finally: p.publish(g_exchange, "test1", "quit") p.close() # 关闭生产者连接 c.join() # 等待消费线程结束