Newbie: how to wait for callbacks...

Jan Bakuwel jan.bakuwel at
Fri Oct 13 18:38:53 CEST 2006

Hoi all,

I'm trying to write a little code that waits for a callback routine to
be called, ideally with a timeout...

I guess the code below is not right (using a boolean flag), but since
I'm new to Python, I don't know yet where the semaphores live and/or
whether I'm on the right track...

Any help is much appreciated...


def processEmail(emailMessage):

    sendSent = False

    def sendComplete(result):
        print "Message sent successfully"
        processEmail.sendSent = True
    #end sendComplete

    def sendFailed(error):
        print >> sys.stderr, "Error", error.getErrorMessage()
        processEmail.sendSent = True
    #end sendFailed

    if emailMessage.is_multipart():
        print "...multipart"
        for pl in emailMessage.walk():
        print "...not multipart"
    # simulate some processing that will take place here

    multiMessage = MIMEMultipart()
    multiMessage['From'] = emailMessage['From']
    multiMessage['To'] = emailMessage['To']
    multiMessage['Subject'] = emailMessage['Subject']
    messageData = multiMessage.as_string(unixfrom=False)
    sending = smtp.sendmail('smtp', 't at t.t', 't at t.t', messageData)

    while not processEmail.sendSent:
        print "z"
    #end while

    print "...done"
    #raise smtp.SMTPDeliveryError(code=550, resp="Unable to deliver
email, please try again later", isFatal=True, retry=False)
    print ""

#end ProcessEmail

More information about the Python-list mailing list