[Twisted-Python] Welcome (and some questions)

Jp Calderone exarkun at divmod.com
Sat Sep 10 19:31:36 EDT 2005

On Sun, 11 Sep 2005 00:37:19 +0200, Marcin Kasperski <marcin.kasperski at softax.com.pl> wrote:
>As it is my first post to the list, I would like to make thanks to all the 
>people who created twisted. Great framework with many useful features. Thank 
>you very much.

Hello, and welcome to the list :)

>I have also some questions, thanks in advance for any hints.
>1) Which logging method is recommended for twisted applications? Python 
>logging package? Some method present in twisted itself? Something else?

Twisted includes the basic pieces needed to construct a logging system.  The common thing to do in a Twisted application is just to call twisted.python.log.msg() and twisted.python.log.err().  These aren't covered much in the documentation, but their usage is pretty straightforward: msg() takes a string and gives all the "log observers" a chance to do something with it; similarly, err() takes a Failure (or nothing, in the suite of an "except:" - it will construct a Failure based on the exception being handled) and does the same thing.

If you use `twistd', there is a log observer which records these events to a file on disk named "twistd.log" and does some rudamentary log rotation.  You can write your own log observer as well, and install it using twisted.python.log.addObserver().  Most applications don't need this, at least not right off the bat.

If you aren't using `twistd', you can start the logging system off with twisted.python.log.startLogging().  It takes a file-like object and records messages to it.

>2) What database access method is recommended? If I understand correctly it 
>would be good idea to execute database access code in many short steps, 
>deferring from time to time. Which database API support this way best, are 
>there any code examples available?

For access to relational databases with an SQL interface, Twisted provides ADB-API: this is a wrapper around any DB-API module which presents a uniform, asynchronous interface (of course, since DB-API modules are blocking, this is achieved via use of a thread pool to issue database requests).  You'll find this at twisted.enterprise.adbapi, and documentation at <http://twistedmatrix.com/projects/core/documentation/howto/enterprise.html> and <http://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.html>.

There has also been some effort towards implementing threadless asynchronous database adapters, but so far none of it is part of Twisted.  You can find a new PostgreSQL adapter like this at <http://www.jamwt.com/pgasync/>.

>3) So far I code using 'low level twisted' and run my application as 
>standalone python script. I understand that it is recommended to migrate to 
>'application/service' API and use twistd to run application. Unfortunately I 
>find it difficult to fully understand documentation on this subject, 
>probably partially because my  application is not TCP/IP server, but rather 
>TCP/IP client (in fact it is some kind of bot using telnet connection to 
>game server). Does it make sense to use application and service concept in 
>such situation? If so, how could the code look like? Currently my 'main' 
>code looks so:
>     reactor.connectTCP(HOST, PORT, MyFactory(...))
>     reactor.run()

`twistd' is the "Twisted daemonizer".  If your application will run as a daemon, whether it is conceptually a server or a client, you'll probably want to use it.

The above example can be trivially transformed into Twisted Service-using code:

    from twisted.application import service, internet

    # The name `application' is special in this arrangement
    application = service.Application("Some Kind of Thingy")

    HOST = 'foo'
    PORT = 23

    # This name is not
    _client = internet.TCPClient(HOST, PORT, MyFactory(...))

Put this into a file named `something.tac' and run it with `twistd -noy something.tac'.  One possible drawback here is that you will not be able to pass values for HOST and PORT as command-line arguments, since .tac files are intended to *be* configuration, not to take configuration.  If these values are generally fixed, this may be fine.  Otherwise, you would probably benefit from the mythical twistd-refactoring, stories of the coming of which have been passed down for generations.  When it comes to pass, you'll be able to take advantage of twistd's functionality in ways other than by writing .tac files (or any of their kin).

>4) Is twisted documentation available anywhere in the form of MsHelp file (I 
>saw a lot of Python documentation converted this way, it is nice as one can 
>easily find information by text search)?

There are no MsHelp-format versions of the documentation that I know of.  We use a tool called `lore' to generate the rendered versions of the Twisted documentation.  Lore supports output plugins, so if someone were interested and motivated, an MsHelp plugin could probably be added, and then all of Twisted's documentation could be made available in that format.

>5) Does there exist example of twisted application with wxpython GUI?

wxPython is a challenge to integrate with.  There have been several strategies attempted and discarded over the years.  The current popular idea involves a new reactor which lets a foreign event loop maintain more control over the program's execution.  There has been no Twisted release since this was added though, so if you want to try it out you'll need to get an SVN checkout of Twisted.  If you do, you'll find some examples in doc/core/examples/threadedselect/.  You can also browse these online at <http://cvs.twistedmatrix.com/cvs/trunk/doc/core/examples/threadedselect/>.

Most Twisted developers (that is, developers of Twisted) prefer Gtk, and Twisted has quite good PyGtk integration.

Hope this helps,


More information about the Twisted-Python mailing list