简体   繁体   中英

python paho mqtt run client for x minutes

I want to run my paho mqtt client for specified period of time and not forever. What is the right way to implement it?

PS I want a blocking call and not the event driven loop_start()/stop() facility

Thanks!

You have to use the event loop or it just won't work.

So your best bet is to implement your own loop and keep track of time. eg

startTime = time.time()
runTime = 5 * 60
while True:
  mqttc.loop()
  currentTime = time.time()
  if (currentTime - startTime) > runTime:
    break

This should run for 5 mins

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM