[TransWarp] PEAK + Twisted?

Phillip J. Eby pje at telecommunity.com
Wed Jul 9 14:58:42 EDT 2003

At 11:20 AM 7/9/03 -0700, Joel Boehland wrote:
>This was resent using a different "From:" address because my previous one 
>email was sent from an address not on the list. I apologize for any duplicates.
>This is an extension to a discussion that was taking place on the #peak 
>irc channel. Some of us were thinking it would be a good practical 
>excercise to "PEAK-ify" a simple twisted app to showcase the two coolest 
>python frameworks working together It would also provide a quickpath for 
>potential developers on the best way to integrate the two frameworks.
>Going from there, we thought the best twisted app to PEAK-ify would be the 
>simple echoserver located in twisted's doc/examples directory.


>Sooo-- the question to the PEAK community is what is the best way to make 
>this twisted protocol part of a component in a PEAK application? I can see 
>that there is some twisted support in the peak.running.interfaces, and 
>peak.running.scheduler modules, but I'm having a hard time wrapping my 
>head around the intended way they should be used.

class EchoRunner(binding.Component):

     reactor = binding.bindTo(running.ITwistedReactor)
     protocol = Echo
     port = 8000

     def __onStart(self, d, a):
         f = Factory()
         f.protocol = self.protocol
         self.reactor.listenTCP(self.port, f)

     __onStart = binding.whenAssembled(__onStart)

Now, define a ZConfig schema component "sectiontype" for this, that uses 
EchoRunner.fromZConfig as its datatype, and declaring that it "implements" 
'running.Task'.   See 'component.xml' in 'peak.running' for 
examples.  Last, but not least, define a new ZConfig schema based on 
'EventDriven.xml', that imports your schema component as well as the 
original data.  Now, you can make a config file like:

#!invoke peak EventDriven
Port 8080

(Actually, instead of 'EventDriven' you'll have a schema URL or shortcut to 
your new ZConfig schema.)

This would be the canonical way to create an event-driven server 
application.  By including other component types in your schema, you can 
have as many different servers running as you like, all started and run 
from the same configuration file.

>The rough path that I can see, using the bulletins example app as a 
>reference is to have a echoapp.ini with stuff like this:
>reactor = getTwistedReactorSomehow()....
>factory = getEchoProtocolComponentSomehow()...
>app = peak.running.scheduler.MainLoop instance???
>Anyone have any comments on this approach? Even better-- has someone 
>already made a simple PEAK+twisted app they'd like to show off to other 
>PEAK developers?

If you just want a quick-and-dirty launcher without nice configuration, you 
can do this:

from peak.running.commands import EventDriven

class EchoCommand(EventDriven):
      commands = binding.New(EchoRunner, activateUponAssembly=True)

And then to run your program:

peak import:mypkg.EchoCommand

That's about it.

More information about the PEAK mailing list