IrcHighlightsFiveTips |
UserPreferences |
The PEAK Developers' Center | FrontPage | RecentChanges | TitleIndex | WordIndex | SiteNavigation | HelpContents |
Courtesy of pje:
1)
If you have a bunch of properties in a namespace that you need to bind to, you can do something like this: myapp = binding.Make(lambda: config.Namespace('myapp')) And then all the things that use the property namespace can bind to it, e.g.: port = binding.Obtain('myapp/receiver-port') user = binding.Obtain('myapp/user') etc. In fact, if your items are all in a "flat" namespace like that, and the items map precisely to the same property names, you can actually use Delegate: user = password = resource = host = binding.Delegate('myapp') So that was tip #1.2) Tip #2...
don't import the Twisted reactor directly. Instead, use reactor = binding.Obtain(running.ITwistedReactor) This will ensure that any PEAK components will end up using the Twisted reactor, instead of PEAK's mini-reactor. E.g., if you use AdaptiveTask, or FastCGIRunner, or stuff like that.3) Tip #3...
don't override AbstractCommand.run(), override the _run() method instead. run() now has handy error trapping, and it may someday have some other helpful hooks. You can just override _run() to define the "meat" of your command-line app.4) Tip #4, and this is kind of specific to Twisted...
Use assembly events to set up listeners and tasks. Specifically, in the case of the Jabber and Site objects... You can set up binding.Make(lambda self: self.doSomething(), uponAssembly=True) bindings in these kinds of services that will cause them to register or start or listenTCP or whatever. They should have a 'reactor = binding.Obtain(running.ITWistedReactor)' as well, and use that to hook up to the reactor. A component that uses them can either delay creation (by not touching the binding), or define the binding as uponAssembly, in which case they'll start ASAP.5) Tip #5...
instead of creating a custom app class, use running.commands.EventDriven. This can be as simple as creating an instance of EventDriven(components=[svc1(), svc2(), ...]) EventDriven has configuration properties for things like automatically shutting down after a time period, or based on how long the app's been idle. There's also a ZConfig schema for EventDriven, which means that you can create ZConfig schema extensions to describe your new services.