How do you you run a Twisted application via Python (instead of via Twisted)?

10,009

Solution 1

I don't know if it's the best way to do this but what I do is instead of:

application = service.Application("chatserver")
internet.TCPServer(1025, factory).setServiceParent(application)

you can do:

from twisted.internet import reactor
reactor.listenTCP(1025, factory)
reactor.run()

Sumarized if you want to have the two options (twistd and python):

if __name__ == '__main__':
    from twisted.internet import reactor
    reactor.listenTCP(1025, factory)
    reactor.run()
else:
    application = service.Application("chatserver")
    internet.TCPServer(1025, factory).setServiceParent(application)

Hope it helps!

Solution 2

Don't confuse "Twisted" with "twistd". When you use "twistd", you are running the program with Python. "twistd" is a Python program that, among other things, can load an application from a .tac file (as you're doing here).

The "Twisted Command Prompt" is a Twisted installer-provided convenience to help out people on Windows. All it is doing is setting %PATH% to include the directory containing the "twistd" program. You could run twistd from a normal command prompt if you set your %PATH% properly or invoke it with the full path.

If you're not satisfied with this, perhaps you can expand your question to include a description of the problems you're having when using "twistd".

Solution 3

On windows you can create .bat file with your command in it, use full paths, then just click on it to start up.

For example I use:

runfileserver.bat:
C:\program_files\python26\Scripts\twistd.py -y C:\source\python\twisted\fileserver.tac

Solution 4

Maybe one of run or runApp in twisted.scripts.twistd modules will work for you. Please let me know if it does, it will be nice to know!

Solution 5

I haven't used twisted myself. However, you may try seeing if the twistd is a python file itself. I would take a guess that it is simply managing loading the appropriate twisted libraries from the correct path.

Share:
10,009
Mike Trpcic
Author by

Mike Trpcic

I am a software developer located in San Francisco specializing in Web Applications and Business Solutions. I am currently focused on using Ruby on Rails to build dynamic web applications, but have experience with a multitude of other technologies. Feel free to browse my website for more information.

Updated on June 03, 2022

Comments

  • Mike Trpcic
    Mike Trpcic almost 2 years

    I am working my way through learning Twisted, and have stumbled across something I'm not sure I'm terribly fond of - the "Twisted Command Prompt". I am fiddling around with Twisted on my Windows machine, and tried running the "Chat" example:

    from twisted.protocols import basic
    
    class MyChat(basic.LineReceiver):
        def connectionMade(self):
            print "Got new client!"
            self.factory.clients.append(self)
    
        def connectionLost(self, reason):
            print "Lost a client!"
            self.factory.clients.remove(self)
    
        def lineReceived(self, line):
            print "received", repr(line)
            for c in self.factory.clients:
                c.message(line)
    
        def message(self, message):
            self.transport.write(message + '\n')
    
    
    from twisted.internet import protocol
    from twisted.application import service, internet
    
    factory = protocol.ServerFactory()
    factory.protocol = MyChat
    factory.clients = []
    
    application = service.Application("chatserver")
    internet.TCPServer(1025, factory).setServiceParent(application)
    

    However, to run this application as a Twisted server, I have to run it via the "Twisted Command Prompt", with the command:

    twistd -y chatserver.py
    

    Is there any way to change the code (set Twisted configuration settings, etc) so that I can simply run it via:

    python chatserver.py
    

    I've Googled, but the search terms seem to be too vague to return any meaningful responses.

    Thanks.

  • Mike Trpcic
    Mike Trpcic over 14 years
    That file, twistd.py, seems very small. Is there any way to include that in the beginning of an application so that I can just run it via the regular python exe?
  • Mike Trpcic
    Mike Trpcic over 14 years
    This was EXACTLY what I was looking for. Thanks a ton.
  • Glyph
    Glyph over 13 years
    In newer versions of Twisted, you should usually be using endpoints rather than directly calling reactor methods. In this example, replace the 'listenTCP' line with <twistedmatrix.com/documents/10.1.0/api/…>. This is more flexible, because you can pass around endpoints without necessarily starting them. In newer versions of Twisted, there will be more and more tools for working with endpoints, too, so it's a bit more future-proof.
  • Chris
    Chris about 8 years
    What about deploying this with your package. Ideally I would like to start my server with a single command after it is installed in a virtualenv. What is the recommended way to ship your server script along with your package?
  • ssc
    ssc almost 8 years
    @Glyph: your link 404s; I think this is the latest version of the document that was meant
  • Glyph
    Glyph almost 8 years
    @ssc - yep, that's the correct link. Thanks for the fix; I'd edit my comment but SO only lets you edit comments that were relatively recently posted.