[Twisted-Python] Novice question on twisted framework
Jean-Paul Calderone
exarkun at divmod.com
Tue Mar 17 07:54:50 MDT 2009
On Tue, 17 Mar 2009 15:18:31 +1100, Sury Soni <ssoni at nextdigital.com> wrote:
>Hi,
>
>I have an existing application, written using Ice Middleware
>(www.zeroc.com)
>
>This application is my source of infinite queue (server)
>
>Following is my application (processing client to my infinite queue
>server) idea to be implemented using Twisted Framework.
>
>I can loop through my infinite queue and receive email data (Using
>.next() kind of function)
>
>Using this email data object, I need to trigger event to send email
>using ESMTPSenderFactory and ESMTPSender. As, later I would be extending
>ESMTP* classes to do my little funky stuff.
>
>What is the best code design I can use to implement this stuff.
Your code looks close. The biggest issue is the loop over the queue.
Instead, you need to handle items from the queue without blocking.
>
>Following is basic algorithm kind of code I am doing right now, but I
>feel I am missing something.
>
>#================
>
>#Code Begins Here: .tac file
>
>#================
>
>
>
>import StringIO
>
>
>
>from twisted.application import service
>
>
>
>from twisted.application import internet
>
>from twisted.internet import protocol, defer
>
>from twisted.mail import smtp, relaymanager
>
>from twisted.internet import reactor
>
>
>
>Def process_queue:
>
The following loop:
> While True:
>
> Try:
>
> Data = my_queue.next()
>
>
>
> getMailExchange('localhost).addCallback(cbMailExchange,
>data)
>
> Except:
>
> Break
>
Needs to be replaced. I'm not sure what my_queue is, but you need to
replace it with something which will call your code when there is an
item to process. Then you can get rid of the loop and define a callback
which looks up a mail exchange and sends a message to it.
You may also want to use twisted.mail.smtp.sendmail rather than the
factories. It's a bit higher-level and simpler.
Jean-Paul
More information about the Twisted-Python
mailing list