Newbie: how to wait for callbacks...

Jan Bakuwel jan.bakuwel at int.greenpeace.org
Fri Oct 13 12:38:53 EDT 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...

Jan


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():
            pass
    else:
        print "...not multipart"
    # simulate some processing that will take place here
    time.sleep(1)

    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)
    sending.addCallback(sendComplete).addErrback(sendFailed)

    while not processEmail.sendSent:
        print "z"
        time.sleep(0.1)
    #end while

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

#end ProcessEmail



More information about the Python-list mailing list