1

The following queue is not working properly somehow. Is there any obvious mistake I have made? Basically every incoming SMS message is put onto the queue, tries to send it and if it successful deletes from the queue. If its unsuccessful it sleeps for 2 seconds and tries sending it again.

# initialize queue
queue = queue.Queue()

def messagePump():
    while True:
        item = queue.get()
        if item is not None:
            status = sendText(item)
            if status == 'SUCCEEDED':
                queue.task_done()
            else:
                time.sleep(2)

def sendText(item):
    response = getClient().send_message(item)
    response = response['messages'][0]
    if response['status'] == '0':
        return 'SUCCEEDED'
    else:
        return 'FAILED'

@app.route('/webhooks/inbound-sms', methods=['POST'])
def delivery_receipt():
    data = dict(request.form) or dict(request.args)
    senderNumber = data['msisdn'][0]
    incomingMessage = data['text'][0]
    # came from customer service operator
    if (senderNumber == customerServiceNumber):
        try:
            split = incomingMessage.split(';')
            # get recipient phone number
            recipient = split[0]
            # get message content
            message = split[1]
            # check if target number is 10 digit long and there is a message
            if (len(message) > 0):
                # for confirmation send beginning string only
                successText = 'Message successfully sent to: '+recipient+' with text: '+message[:7]
                queue.put({'from': virtualNumber, 'to': recipient, 'text': message})

The above is running on a Flask server. So invoking messagePump:

thread = threading.Thread(target=messagePump)
thread.start()
configureMe
  • 41
  • 1
  • 5

1 Answers1

0

The common in such cases is that Thread has completed execution before item started to be presented in the queue, please call thread.daemon = True before running thread.start().

Another thing which may happen here is that Thread was terminated due to exception. Make sure the messagePump handle all possible exceptions.

That topic regarding tracing exceptions on threads may be useful for you:

Catch a thread's exception in the caller thread in Python

Andriy Ivaneyko
  • 20,639
  • 6
  • 60
  • 82