Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Paho » Problem with multiple clients in one python process(Problem having multiple clients seems broken)
Problem with multiple clients in one python process [message #1833065] Thu, 01 October 2020 16:03 Go to next message
Lee Butler is currently offline Lee ButlerFriend
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 107 times)
Re: Problem with multiple clients in one python process [message #1833081 is a reply to message #1833065] Thu, 01 October 2020 19:57 Go to previous messageGo to next message
Ian Craggs is currently offline Ian CraggsFriend
Messages: 83
Registered: March 2013
Member
That's exactly the symptom you would get if you use the same MQTT client id for both connections. The second one will bump off the first, and so on. Use different MQTT client ids for each instance.
Re: Problem with multiple clients in one python process [message #1833178 is a reply to message #1833081] Mon, 05 October 2020 16:41 Go to previous message
Lee Butler is currently offline Lee ButlerFriend
Messages: 2
Registered: October 2020
Junior Member
You rock. I feel a fool for not having noticed that. Thanks.
Previous Topic:MQTT Server publish scheduler
Next Topic:MQTTClient_connect() failure
Goto Forum:
  


Current Time: Fri May 03 09:20:50 GMT 2024

Powered by FUDForum. Page generated in 0.03578 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top