Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4028608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:14:28+00:00 2026-05-20T11:14:28+00:00

Background I’ve recently been a part of a project where twisted was used. We

  • 0

Background

I’ve recently been a part of a project where twisted was used. We utilized a TimerService to daemonize a process. And yes, I realize that this approach may have been overkill, but we’re trying to stay consistent and use a proven framework. Yesterday, an exception went unhandled within the LoopingCall which caused the TimerService to fail, but the twistd application was still running (see twisted enhancement request). To avoid this, we would like to stop the service at the end of a catch-all exception handler.

Question

How to stop both the TimerService and the Twistd application from within the LoopingCall callable method? My concern is that the linux process keeps running when the TimerService is unable to handle an exception, even though the TimerService isn’t looping anymore.

For example:


def some_callable():
  try:
    # do stuff
  except SomeSpecificError ex:
    # handle & log error
  except SomeOtherSpecificError ex:
    # handle & log error
  except:
    # log sys.exc_info() details
    # stop service.

NOTE: The following does not work within the callable.


from twisted.internet import reactor
reactor.stop()
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:14:29+00:00Added an answer on May 20, 2026 at 11:14 am

    You can’t stop the reactor before it starts:

    >>> from twisted.internet import reactor
    >>> reactor.stop()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/base.py", line 570, in stop
        "Can't stop reactor that isn't running.")
    twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.
    >>> 
    

    However, as long as the reactor is running already, reactor.stop works fine:

    >>> from twisted.internet import reactor
    >>> reactor.callLater(3, reactor.stop)
    <twisted.internet.base.DelayedCall instance at 0xb762d2ec>
    >>> reactor.run()
    [... pause ...]
    >>> 
    

    TimerService is a wrapper around LoopingCall. And more specifically, when it starts its LoopingCall, it passes now=True to run. That causes the function to be called the first time immediately, rather than after the specified interval elapses once.

    So when TimerService.startService is called, your function is called. And the reactor isn’t running yet. On that first call to your function, you can’t stop the reactor, because it hasn’t been started.

    This program:

    from twisted.application.internet import TimerService
    
    def foo():
        from twisted.internet import reactor
        reactor.stop()
    
    from twisted.application.service import Application
    
    application = Application("timer stop")
    TimerService(3, foo).setServiceParent(application)
    

    produces these results:

    exarkun@boson:/tmp$ twistd -ny timerstop.tac
    2011-03-08 11:46:19-0500 [-] Log opened.
    2011-03-08 11:46:19-0500 [-] using set_wakeup_fd
    2011-03-08 11:46:19-0500 [-] twistd 10.2.0+r30835 (/usr/bin/python 2.6.4) starting up.
    2011-03-08 11:46:19-0500 [-] reactor class: twisted.internet.selectreactor.SelectReactor.
    2011-03-08 11:46:19-0500 [-] Unhandled Error
            Traceback (most recent call last):
              File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/application/service.py", line 277, in startService
                service.startService()
              File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/application/internet.py", line 284, in startService
                self._loop.start(self.step, now=True).addErrback(self._failed)
              File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/task.py", line 163, in start
                self()
              File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/task.py", line 194, in __call__
                d = defer.maybeDeferred(self.f, *self.a, **self.kw)
            --- <exception caught here> ---
              File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/defer.py", line 133, in maybeDeferred
                result = f(*args, **kw)
              File "timerstop.py", line 5, in foo
                reactor.stop()
              File "/home/exarkun/Projects/Twisted/branches/simplify-ssl-4905/twisted/internet/base.py", line 570, in stop
                "Can't stop reactor that isn't running.")
            twisted.internet.error.ReactorNotRunning: Can't stop reactor that isn't running.
    

    However, this one works fine:

    from twisted.application.internet import TimerService
    
    counter = 0
    
    def foo():
        global counter
        if counter == 1:
            from twisted.internet import reactor
            reactor.stop()
        else:
            counter += 1
    
    from twisted.application.service import Application
    
    application = Application("timer stop")
    TimerService(3, foo).setServiceParent(application)
    

    And slightly less grossly, so does this one:

    from twisted.application.internet import TimerService
    
    def foo():
        from twisted.internet import reactor
        reactor.callWhenRunning(reactor.stop)
    
    from twisted.application.service import Application
    
    application = Application("timer stop")
    TimerService(3, foo).setServiceParent(application)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I've taken over a very large asp.net website project. The old deploy process
Background: We're building an application that allows our customers to supply data in a
Background: I would like to dismiss a modalView that I have presented earlier and
Background I have a rails 3 app that has a model named A with
Background I am trying to build a Java program that make use of an
Background - I have a webpage that contains a bunch of buttons (think POS
Background: I have a UITableView which has three rows with custom UITextFields that are
Background I have a SQL dataset that is called as a view via LINQ-to-Entities.
Background: I'm working on a program that needs to be able to capture the
Background: I've wrote a small library that is able to create asp.net controls from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.