modify python code as newbie
ron.eggler at ecoation.com
ron.eggler at ecoation.com
Fri Jan 17 19:41:03 EST 2020
Hi,
I'm semi new to Python but need to modify a program that calls the mqtt_client.publish() function from aws iot.
Now, when the publish function fails, it raises an exception. I need to change the code so that when an exception is raised, instead of giving up, it should retry (indefinitely).
Here's some semi pseudo code of what I came up with:
What I'm particularly interested in is, if when the exception in pub_msg() is raised, will my thread t keep running?
What else could/should be improved in the below snippet? I'm looking forward to get some inputs.
Thanks!
import retrying
import Queue as queue
import threading as th
NUM_THREADS=1
nth=[]
def worker():
while not_terminated():
item = q.get()
if item is None:
continue
do_stuff(item)
def enQ(self, msg, topic):
if len(nth<int(NUM_THREADS):
t = th.Thread(target=worker)
t.start()
nth.append(t)
q.put([self,msg,topic])
def do_stuff(dat):
self = dat[0]
msg = dat[1]
topic = dat[2]
pub-msg(slef, msg, topic)
def send_msg(self, msg,topic):
enQ(self,msg,topic)
def pub_msg(self,msg,topic):
try:
if topic == "test" and \
something[self.xyz]:
return
except KeyError as err:
foo("Exception {}".format(err))
msg_json=bar(topic,msg)
aws_publish(self,msg_json,topic)
@retry (wait_rndom_min=1000, wait_rndom_max=2000)
def aws_publish(self.msg_json,topic):
self.__mqtt_client.publish(
"/{}/{}".format(topic, self._uuid), msg_json, 1)
More information about the Python-list
mailing list