Problem with multiple clients in one python process [message #1833065] |
Thu, 01 October 2020 16:03 |
Lee Butler Messages: 2 Registered: October 2020 |
Junior Member |
|
|
There seems to be a problem having multiple clients in a single process using the Paho MQTT python implementation. What is the appropriate or anticipated programming model to use?
My class constructor allocates a client and connects to a broker. As long as only a single instance is constructed, all works well. If a second instance of the class is instantiated, then clients seem to spontaneously disconnect and reconnect.
consider the following topic & message pair sequence published with QoS 2:
door/knob one
door/knob two
door/knock three
door/knock four
DAC/shutdown five
DAC/shutdown six
DAC/shutdown seven
DAC/shutdown eight
DAC/shutdown nine
Note that the DAC/shutdown message topic here is designed to signal client shutdown, disconnect, and process termination.
For a single instance, the class instance sees a single on_connect invocation, and messages "one" through "five" are received as anticipated. The log is:
Quote:
hello
main thread t_484612
spy1 t_382308 connected None {'session present': 0} 0
spy1 t_382308 msg door/knob one
spy1 t_382308 msg door/knob two
spy1 t_382308 knock door/knock three
spy1 t_382308 knock door/knock four
spy1 t_382308 on_shutdown DAC/shutdown five
spy1 t_382308 on_disconnect None 0
spy1 log
t_382308 door/knob one
t_382308 door/knob two
t_382308 door/knock three
t_382308 door/knock four
t_382308 DAC/shutdown five
If a second instance of the class is constructed, then the clients rapidly and spontaneously disconnect and connect. Likewise, only the client which is currently connected receives published messages. The log becomes:
Quote:
hello
spy1 t_478900 connected None {'session present': 0} 0
main thread t_492328
spy2 t_473184 connected None {'session present': 0} 0
spy1 t_478900 disconnect without shutdown
spy1 t_478900 connected None {'session present': 0} 0
spy2 t_473184 disconnect without shutdown
spy1 t_478900 on_connect while was not connecting
spy1 t_478900 disconnect without shutdown
spy2 t_473184 connected None {'session present': 0} 0
spy2 t_473184 on_connect while was not connecting
spy1 t_478900 connected None {'session present': 0} 0
spy2 t_473184 disconnect without shutdown
spy1 t_478900 on_connect while was not connecting
spy1 t_478900 disconnect without shutdown
spy2 t_473184 connected None {'session present': 0} 0
spy2 t_473184 on_connect while was not connecting
spy2 t_473184 msg door/knob two
spy2 t_473184 knock door/knock three
spy1 t_478900 connected None {'session present': 0} 0
spy2 t_473184 disconnect without shutdown
spy1 t_478900 on_connect while was not connecting
spy1 t_478900 knock door/knock four
spy1 t_478900 on_shutdown DAC/shutdown five
spy1 t_478900 on_disconnect None 0
shutting_down [True, False]
spy2 t_473184 connected None {'session present': 0} 0
spy2 t_473184 on_connect while was not connecting
spy2 t_473184 on_shutdown DAC/shutdown seven
spy2 t_473184 on_disconnect None 0
spy1 log
t_478900 door/knock four
t_478900 DAC/shutdown five
spy2 log
t_473184 door/knob two
t_473184 door/knock three
t_473184 DAC/shutdown seven
Observation:
Only the client "connected" gets messages.
So, what is the proper way to have multiple clients within a single process?
Any hint would be welcome.
-
Attachment: multiple.py
(Size: 5.54KB, Downloaded 114 times)
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02785 seconds