Modify Python code as newbie
ron.eggler at ecoation.com
ron.eggler at ecoation.com
Fri Jan 17 19:34:25 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, if 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.
Here's some semi pseudo code of what I came up with and what I'm particularly interested in is, if the exception in pub_msg() is raised, will my thread t keep running?
What else could/should be improved about 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
numth=[]
def worker():
while not_terminated():
item = q.get()
if item is None:
continue
do_stuff(item)
def enQ(self, msg, topic):
if len(numth<int(NUM_THREADS):
t = th.Thread(target=worker)
t.start()
numth.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))
aws_publish(self,msg,topic)
@retry (wait_random_min=1000, wait_random_max=2000)
def aws_publish(self.msg,topic):
self.__mqtt_client.publish(
"/{}/{}".format(topic, self._uuid), msg_json, 1)
More information about the Python-list
mailing list