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

  • Home
  • SEARCH
  • 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 8403975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:28:35+00:00 2026-06-09T22:28:35+00:00

I have just started to learn twisted and wrote a small tcp server/client using

  • 0

I have just started to learn twisted and wrote a small tcp server/client using the Tcp4endpoint-class. Everything works fine, except for one thing.

In order to detect the event that an unavailble port is given to the server as listen port, I have added an errback to the endpoint-deferer. This errback is triggered, however, I am unable to exit the application from the errback. Reactor.stop causes another failure saying that reactor is not running, while for example sys.exit triggers another error. The output from the two latter is only seen when I do ctrl+c and gc hits.

My question is, is there any way to make the application exit (cleanly) after a listenFailure has occured?

  • 1 1 Answer
  • 0 Views
  • 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-06-09T22:28:36+00:00Added an answer on June 9, 2026 at 10:28 pm

    A minimal example would help make your question more clear. However, based on many years of Twisted experience, I have an educated guess. I think you wrote a program something like this:

    from twisted.internet import endpoints, reactor, protocol
    
    factory = protocol.Factory()
    factory.protocol = protocol.Protocol
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000)
    d = endpoint.listen(factory)
    def listenFailed(reason):
        reactor.stop()
    d.addErrback(listenFailed)
    
    reactor.run()
    

    You are on the right track. Unfortunately you have an ordering issue. The reason reactor.stop fails with ReactorNotRunning is that the listen Deferred fails ”before” you call reactor.run. That is, it has already failed by the time you did d.addErrback(listenFailed), so listenFailed was called right away.

    There are a number of solutions to this. One is to write a .tac file and use services:

    from twisted.internet import endpoints, reactor, protocol
    from twisted.application.internet import StreamServerEndpointService
    from twisted.application.service import Application
    
    application = Application("Some Kind Of Server")
    
    factory = protocol.Factory()
    factory.protocol = protocol.Protocol
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000)
    
    service = StreamServerEndpointService(endpoint, factory)
    service.setServiceParent(application)
    

    This is run using twistd, like twistd -y thisfile.tac

    Another option is to use the low-level feature that services are based on, reactor.callWhenRunning:

    from twisted.internet import endpoints, reactor, protocol
    
    factory = protocol.Factory()
    factory.protocol = protocol.Protocol
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8000)
    
    def listen():
        d = endpoint.listen(factory)
        def listenFailed(reason):
            reactor.stop()
        d.addErrback(listenFailed)
    
    reactor.callWhenRunning(listen)
    reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started to learn the very basics of Java programming. Using a
I've just started to learn about tie . I have a class named Link
i have just started to learn MVC 4 ASP.NET. I am using a tutorial
I am using android.com tutorial to learn android using eclipse. I have just started
I'have just started to learn PHP, I'm using a free host to test my
I have just started to Learn the Json and binding data to Gridview using
I have just started to learn how to code iOS apps. I have made
I have just started learning Rails, is it required for me to learn CoffeeScript
we have just started using a git account of our Django website project so
I have just started using RSpec and I copied the very simple test on

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.