[Twisted-Python] question : dbus for twistd application framework

Daniel Bryan danbryan at gmail.com
Wed May 30 20:43:55 MDT 2012

Here's an example based on an application that I have in production.
It's a bit amateurish - and I'm going to mangle the language - but you
should get the idea.

This is an extract from the main script:

Class Handler(Resource):
        pass # application code goes here - HTTP request handling, etc.

def get_handler():
	handler = Handler()
	factory = server.Site(handler)
	return factory

if __name__ == '__main__':
	from twisted.internet import reactor
	factory = get_handler()
	reactor.addSystemEventTrigger('before', 'shutdown', cleanup)
	reactor.listenTCP(8080, factory)

If you run the above script, it will run Twisted from a trapped terminal.

For deployment, I've built a server.tac file in the same directory:

import twisted.application.service as Service
from twisted.application.service import IProcess
from twisted.application.internet import TCPServer
# append the working directory to sys.path so that we can import the
handler module
import sys
import os

import handler

factory = handler.get_handler()
application = Service.Application('Data Collector')
IProcess(application).processName = 'data-collector'
TCPServer(8080, factory).setServiceParent(application)

I can then run this command:

twistd -y server.tac

And it'll start up the server and daemonise the process, running it as
a service.

You can import any code you one in your server.tac script and keep it
in your service. It's really just a very simple abstraction of the
kind of plumbing you need to do to get a process to detach from the
terminal you run it in.

On Thu, May 31, 2012 at 12:27 PM, bino oetomo <bino at indoakses-online.com> wrote:
> Dear All
> c/q Itamar, I really appreciate your enlightment
> On 05/29/2012 09:14 PM, Itamar Turner-Trauring wrote:
>> Applications are just a *deployment* method, providing startup and
>> shutdown notification; they're not really relevant to how you interact
>> with dbus. See
>> http://twistedmatrix.com/documents/current/core/howto/application.html -
>> converting code from reactor.run() to Application/twistd usage just
>> involves creating a Service.
> Kindly please give me example of realy simple reactor.run() type script
> ... and how to turn it to a service and join to a MultiService ?
> Sincerely
> -bino-
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

More information about the Twisted-Python mailing list