njh / ruby-mqtt

Pure Ruby gem that implements the MQTT protocol, a lightweight protocol for publish/subscribe messaging.
http://www.rubydoc.info/gems/mqtt
MIT License
541 stars 135 forks source link

Is ruby-mqtt threadsafe? #103

Closed NidhiArien closed 6 years ago

NidhiArien commented 6 years ago

Hi,

I am writing a ruby module which has 8 threads in it. Before start of each thread, I would initialize an mqtt client using the gem and then store the client in a thread safe variable.

thread = Thread.current           
# Connect to environment specific MQTT client
thread[:mqtt_client_individual_thread] = connect_mqtt_client

With the tests I have in development mode, I am seeing no flags. I wanted to hear from others if they have seen any issues with Threading and ruby-mqtt. Thank you for your help.

Xasin commented 6 years ago

Hi!

I would say it's thread-safe, as the core code seems to be using Queues to handle sending and receiving of packets, as well as multiple synchronisation Mutex. However, I am not 100% sure about this, so it's better if the author could answer.

njh commented 6 years ago

Hi @NidhiArien,

As Xasin says, The Ruby MQTT client itself using threading internally and they should not be referring to any global or class variables. So I am not aware of a reason why you shouldn't be able to run multiple clients in multiple threads, but it is not something I have tried myself.

nick.

njh commented 6 years ago

No reply, closing issue.