Using Threads in Twisted

  1. Introduction
  2. Running code in a thread-safe manner
  3. Running code in threads
  4. Utility Methods
  5. Managing the Thread Pool

Introduction

Before you start using threads, make sure you do at the start of your program:

from twisted.python import threadable
threadable.init()

This will make certain parts of Twisted thread-safe so you can use them safely. However, note that most parts of Twisted are not thread-safe.

Running code in a thread-safe manner

Most code in Twisted is not thread-safe. For example, writing data to a transport from a protocol is not thread-safe. Therefore, we want a way to schedule methods to be run in the main event loop. This can be done using the function factory.fp.write(line+'\n') class LogfileFactory(Factory): protocol = LoggingProtocol def __init__(self, fileName): self.file = fileName def startFactory(self): self.fp = open(self.file, 'a') def stopFactory(self): self.fp.close()

Putting it All Together

So, you know what factories are, and want to run the QOTD with configurable quote server, do you? No problems, here is an example.

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class QOTD(Protocol):

    def connectionMade(self):
        self.transport.write(self.factory.quote+'\r\n')
        self.transport.loseConnection()


class QOTDFactory(Factory):

    protocol = QOTD

    def __init__(self, quote=None):
        self.quote = quote or 'An apple a day keeps the doctor away'

reactor.listenTCP(8007, QOTDFactory("configurable quote"))
reactor.run()

The only lines you might not understand are the last two.

listenTCP is the method which connects a Factory to the network. It uses the reactor interface, which lets many different loops handle the networking code, without modifying end-user code, like this. As mentioned above, if you want to write your code to be a production-grade Twisted server, and not a mere 20-line hack, you will want to use the Application object.

Index

Version: 1.3.0./usr/share/doc/twisted-doc/howto/telnet.html0000644000000000000000000000761010203134333021374 0ustar rootroot00000000000000Twisted Documentation: Using telnet to manipulate a twisted server

Using telnet to manipulate a twisted server

    To start things off, we're going to create a simple server that just gives you remote access to a Python interpreter. We will use a telnet client to access this server.

    Run mktap telnet -p 4040 -u admin -w admin at your shell prompt. If you list the contents of your current directory, you'll notice a new file -- telnet.tap. After you do this, run twistd -f telnet.tap. Since the Application has a telnet server that you specified to be on port 4040, it will start listening for connections on this port. Try connecting with your favorite telnet utility to 127.0.0.1 port 4040.

    $ telnet localhost 4040
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    
    twisted.manhole.telnet.ShellFactory
    Twisted 1.1.0
    username: admin
    password: admin
    >>>
    

    Now, you should see a Python prompt -- >>>. You can type any valid Python code here. Let's try looking around.

    >>> dir()
    ['__builtins__']
    

    Ok, not much. let's play a little more:

    >>> import __main__
    >>> dir(__main__)
    ['__builtins__', '__doc__', '__name__', 'os', 'run', 'string', 'sys']
    
    >>> service
    <twisted.application.internet.TCPServer instance at 0x10270f48>
    >>> service._port
    <twisted.manhole.telnet.ShellFactory on 4040>
    >>> service.parent
    <twisted.application.service.MultiService instance at 0x1024d7a8>
    

    The service object is the service used to serve the telnet shell, and that it is listening on port 4040 with something called a ShellFactory. Its parent is a twisted.application.service.MultiService, a collection of services. We can keep getting the parent attribute of services until we hit the root of all services in this tap.

    As you can see, this is quite useful - we can introspect a running process, see the internal objects, and even change their attributes. We can add telnet support to existing tap like so: mktap --append=foo.tap telnet -p 4040 -u user -w pass. The telnet server can of coursed be used from straight Python code as well. Yo