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 6748629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:35:18+00:00 2026-05-26T12:35:18+00:00

I feel like I am not understanding some things in writing Twisted applications (.tac

  • 0

I feel like I am not understanding some things in writing Twisted applications (.tac files). Using deferred objects in .py scripts is easy by just calling reactor.run() at the end, but I have not seen reactor.run() used in any twisted application sample code.

Can someone explain:

  1. why reactor.run() is not called in twisted applications (or if this is an erroneous conclusion)
  2. how I can use deferred objects inside a twisted application perhaps without calling reactor.run()
  3. and general differences in writing twisted scripts vs applications.
  • 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-05-26T12:35:18+00:00Added an answer on May 26, 2026 at 12:35 pm

    1. Why is reactor.run() not called in example .tac files?

    .tac files are meant to be loaded by the “twistd” command-line tool, which runs the reactor for you.

    Running the reactor is something that’s done once, by whatever bit of code is serving as the main-point for your program. Most Twisted code is effectively a plug-in of some kind, meant to operate in the context of a larger system.

    In the particular case of .tac files, they are never meant to be run as stand-alone Python programs: their job is to produce an Application object (with an attached bunch of Service objects) that get started up when the reactor runs. It’s important that the tac file itself not do much work on its own, because (for example) the Service implementations in question may need to separate code which needs to run privileged and unprivileged, which is an exacting process; if work is performed in the .tac itself, it may be haphazardly executed as the wrong user.

    2. How I can use Deferreds inside a Twisted application, without calling reactor.run()?

    Deferred is simply a mechanism for managing chains of callbacks. You don’t need to call reactor.run(), or indeed, even have a reactor at all, to use them. For example:

    >>> from twisted.internet.defer import Deferred
    >>> d = Deferred()
    >>> def hello(result):
    ...     print "'d' was fired:", result
    ...     return result + 3
    ... 
    >>> d.addCallback(hello)
    <Deferred at ...>
    >>> print d
    <Deferred at ...>
    >>> d.callback(7)
    'd' was fired: 7
    >>> print d
    <Deferred at ... current result: 10>
    

    That said, many APIs that return a Deferred need the reactor to do some work in order to eventually call .callback() on it. For example, if you do …

    >>> from twisted.internet.task import deferLater
    >>> from twisted.internet import reactor
    >>> deferLater(reactor, 1.0, lambda: 20).addCallback(hello)
    <Deferred at ...>
    >>>
    

    … you’ll be sitting around forever waiting for that to fire unless somebody runs the reactor. Nothing will print until that happens.

    But, if the reactor is already running – for example, if you were running this interactive example in python -m twisted.conch.stdio rather than python, you would see that Deferred get called back a second later, because that interactive prompt is already running the reactor.

    3. What are the differences between Twisted scripts vs. applications?

    These aren’t really formally separated terms. Any Python script can potentially import code from Twisted and use it any way it wants to, so it’s hard to say that any particular property applies to “scripts” except that they are computer programs :-).

    If by Twisted Application you mean a .tac file or a plugin, the difference is that this kind of code is separated out into the part the builds the service (the code at the top level in your tac file or plugin) and the part that actually does the work (privilegedStartService/startService/stopService implementations of the services that said top-level code sets up). Also, code that runs in this context (i.e. is driven by twistd) does not need to run the reactor itself, since one will be set up and run by twistd itself. Such code must also therefore be careful to avoid importing twisted.internet.reactor, because twistd provides the ability to use different reactors (select, poll, epoll, kqueue, etc) and importing the reactor yourself before twistd has a chance to set it up will break this feature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like I am not writing this correctly and this is my first
I feel like this is not a very efficient way of using linq. I
I am not an idiot, but header files make me feel like one sometimes.
I feel like this might be really simple but I'm just not getting it
I feel like I'm trying to do something simple but I am not getting
I feel like there's a pretty simple way to do this, but I'm not
I feel like this is something I should already know, but I'm just not
I feel like there's a simple solution to this, but I'm not really sure
I feel like such a noob asking this but, for some reason a horizontal
I tried a few things but this week i feel like my brain's having

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.