pythontwistedtwisted.wordstwisted.application

How to gracefully exit application started with twistd?


I have a jabber client that is reading from its stdin and posting PubSub messages. If I get EOF on stdin, I want to terminate the client.

I first tried sys.exit(), but this causes an exception and the client does not exit. I then did some searching and found out that I should call reactor.stop(), but I am unable to make this work. The following code in my client:

from twisted.internet import reactor
reactor.stop()

Results in exceptions.AttributeError: 'module' object has no attribute 'stop'

What do I need to do to cause twistd to shut my application down and exit?

EDIT 2

The original problem was caused by some symlinks messing up the module import. After fixing that problem, I get a new exception:

twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.

After the exception, twistd shuts down. I think this may be caused by the call to MyClient.loop in MyClient.connectionInitialized. Perhaps I need to defer the call until later?

EDIT

Here's the .tac file for my client

import sys

from twisted.application import service
from twisted.words.protocols.jabber.jid import JID

from myApp.clients import MyClient

clientJID = JID('client@example.com')
serverJID = JID('pubsub.example.com')
password = 'secret'

application = service.Application('XMPP client')
xmppClient = client.XMPPClient(clientJID, password)
xmppClient.logTraffic = True
xmppClient.setServiceParent(application)

handler = MyClient(clientJID, serverJID, sys.stdin)
handler.setHandlerParent(xmppClient)

Which I'm invoking with

twistd -noy sentry/myclient.tac < input.txt

Here's the code for MyClient:

import os
import sys
import time
from datetime import datetime

from wokkel.pubsub import PubSubClient

class MyClient(PubSubClient):
    def __init__(self, entity, server, file, sender=None):
        self.entity = entity
        self.server = server
        self.sender = sender
        self.file = file

    def loop(self):
        while True:
            line = self.file.readline()
            if line:
                print line
            else:
                from twisted.internet import reactor
                reactor.stop()

    def connectionInitialized(self):
        self.loop()

Solution

  • from twisted.internet import reactor
    reactor.stop()
    

    that should work. The fact that it doesn't means something else is wrong on your application. I can't figure out what's wrong from the information you provided.

    Can you provide more (all) of the code?


    EDIT:

    Ok, now the problem is that you don't stop your own while True loop, so it will keep looping and eventually stop the reactor again.

    Try this:

    from twisted.internet import reactor
    reactor.stop()
    return
    

    Now, I suspect your loop isn't very good thing for a event-driven framework. While you're just printing lines, it is fine, but depending on what you want to really do (I suspect you'll do more than just print lines) you'll have to refactor that loop to work with events.