Broken Pipe during execution

Hey guys!

Please feel free to dig into my account and look at the server and error logs.

I have a function that sends a text blast. Regulatory whatnots have led to me using a pool of numbers so my messages get through. From that, I had to tweak my code to include the source of the incoming text, so that every user consistently communicates with the same number.

Problem is, at the bottom of this function my server log starts predictably going crazy. Everyone ends up getting 4 messages over the course of 10 minutes or so, and I get 4 "send_intro complete" over that time.

The only error in the error log is OS: write error. The server log itself is doing its haywire thing, with a "Broken Pipe" reference I don't understand. It looks like something in here is getting stuck and causing it to just run repeatedly until it gives up.


def execute_intro():
    introMessage = '''Hi {name}! This is... and I'd love to have an agent reach out to schedule your trip! Reply "yes" 
to set up a call, "no" to wait, or "stop" if you don't remember purchase.'''

print ("EXECUTE message received")
allUsers = Chat_Users.query.all()
goodCount = 0
badCount = 0

for item in allUsers:
    send_intro(item.user_number, item.user_name)
    item.current_status = "intro"
    item.call_accepted = False
    item.call_time = ""
    item.email_accepted = False
    item.new_email = "" = ""
    item.previous_error = False
    print ("intro sent and properties updated for user ", item.user_number)

    timeSent ="%x at %X")

    sentLogEntry = Chat_Log(
        timestamp = timeSent,
        direction = "outgoing",
        user_number = item.user_number,
        user_name = item.user_name,
        message = introMessage,
        current_status = "intro",
        call_accepted = None,
        call_time = "",
        email_accepted = None,
        new_email = "",
        feedback = "",
        previous_error = False
    goodCount += 1
    print ("sentLogEntry updated for user ", item.user_number)
    print ("wait 5 seconds")
         #   print ("intro/sentLogEntry failed for user ", item.user_number)
         #   badCount += 1
         #   time.sleep(5)
         #   print ("wait 5 seconds")

send_sms("FROM FLASK--send_intro executed. {goodCount} records successfully initiated and {badCount} records failed.".format(goodCount=goodCount,badCount=badCount), "1------", "1------")
    #print ("sending intro failed")
    #send_sms("FROM FLASK--send_intro failed.", "1------", "1------")

outputDict = {
    "good count": goodCount,
    "bad count": badCount

return outputDict

Below is the server log stuff that's telling me it's going crazy. HELP!!

2019-11-30 02:16:51 intro sent and properties updated for user  1------
2019-11-30 02:16:51 sentLogEntry updated for user  1------
2019-11-30 02:16:56 wait 5 seconds
2019-11-30 02:16:56 send_sms reached -- FROM FLASK--send_intro executed. 4 records successfully initiated 
and 0 records failed. -- 1------
2019-11-30 02:16:57 Sat Nov 30 02:16:57 2019 - uwsgi_response_writev_headers_and_body_do(): Broken pipe 
[core/writer.c line 306] during POST /foo (

Your web app has 3 workers. If you're sending the messages at import time, then the message sending code will be run once for each worker as it starts up.

What does that mean, and how do I fix it?

It means that if you have code that runs every time your web app starts, it will be run once for every worker. Make sure that the code that sends the messages is run as part of a request, and not as part of the start up of your web app.

Oh. it is, it's triggered by a post request from my texting service. That's what makes it so weird.

Where would the broken pipe be coming from?

is it always exactly 4 times? or is it sometimes once etc