1

I am a noob to web and mqtt programming, I am working on a python application that uses mqtt (via hivemq or rabbitmq broker) and also needs to implement http rest api for clients.

I realized using python bottle framework is pretty easy to provide a simple http server, however both bottle and mqtt have their event loop, how do I combine these 2 event loop, I want to have a single threaded app to avoid complexity.

user424060
  • 1,545
  • 3
  • 20
  • 29
  • You probably don't. What exactly are you trying to achieve, we need more details – hardillb Dec 21 '16 at 08:07
  • to give you more details, clients connect to this service via http/rest, however this service interacts with many other services on back-end via mqtt. so this service has to run mqtt loop (using paho python client) and to furnish http requests it uses bottle and hence it has to run bottle's run loop. this can be done in 2 different threads, my question is that is there a way to combine these in 1 thread? Do you suggest that using 2 threads is better/only way out ? – user424060 Dec 21 '16 at 08:19
  • I meant more what is the MQTT interaction, is it request/response? – hardillb Dec 21 '16 at 08:31
  • MQTT interaction is completely async, pub/sub, this service never blocks for MQTT responses. – user424060 Dec 21 '16 at 08:35

1 Answers1

1

I'm not familiar with bottle but a quick look at the docs it doesn't look like there is any other way to start it's event loop apart from with the run() function.

Paho provides a loop_start() which will kick off it's own background thread to run the MQTT network event loop.

Given there looks to be no way to run the bottle loop manually I would suggest calling loop_start() before run() and letting the app run on 2 separate threads as there is no way to combine them and you probably wouldn't want to anyway.

The only thing to be careful of will be if MQTT subscriptions update data that the REST service is sending out, but as long as are not streaming large volumes of data that is unlikely to be an issue.

hardillb
  • 54,545
  • 11
  • 67
  • 105