Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('[email protected]')
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()
like image 370
lawnsea Avatar asked Mar 17 '11 19:03

lawnsea


3 Answers

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.

like image 185
nosklo Avatar answered Oct 15 '22 08:10

nosklo


Use reactor.callFromThread(reactor.stop) instead of reactor.stop. This should solve the issue.

like image 35
No Minds Vision Avatar answered Oct 15 '22 09:10

No Minds Vision


I used to do this way (in sigint handler of a non-twistd called application):

reactor.removeAll()
reactor.iterate()
reactor.stop()

I'm not 100% sure it is the right way, but twisted is happy

the same application started in a tac is handled directly by twistd signal handler, I've found this question because I have some rpc client requests that I would to wait for and handle result before exiting and looks like twistd is just killing the reactor without letting the call finish

like image 1
sherpya Avatar answered Oct 15 '22 10:10

sherpya