test_paho.py 942 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import uuid
  2. import library.paho.mqtt.client as mqtt
  3. import threading
  4. connect_count = 0
  5. lock = threading.Lock()
  6. def locust_on_connect(client, flags_dict, userdata, rc):
  7. global lock
  8. global connect_count
  9. print("client: {} connected, rc: {}".format(client, rc))
  10. lock.acquire()
  11. connect_count += 1
  12. print("connect_count: {}".format(connect_count))
  13. lock.release()
  14. def locust_on_subscribe(client, userdata, mid, granted_qos):
  15. print("client: {} subscribed, mid: {}".format(client, mid))
  16. def log(client, userdata, level, buf):
  17. print("[paho-log][client: {}] {}".format(client, buf))
  18. for x in range(0, 342):
  19. client = mqtt.Client(client_id = str(uuid.uuid1()))
  20. host = '127.0.0.1'
  21. port = 1883
  22. client.on_connect = locust_on_connect
  23. client.connect_async(host, port)
  24. client.loop_start()
  25. client.subscribe("/topic", 1)
  26. input("type enter to end")