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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:59:58+00:00 2026-05-16T22:59:58+00:00

What is the difference between defer.execute() and threads.deferToThread() in twisted? Both take the same

  • 0

What is the difference between defer.execute() and threads.deferToThread() in twisted? Both take the same arguments – a function, and parameters to call it with – and return a deferred which will be fired with the result of calling the function.

The threads version explicitly states that it will be run in a thread. However, if the defer version doesn’t, then what would ever be the point of calling it? Code that runs in the reactor should never block, so any function it calls would have to not block. At that point, you could just do defer.succeed(f(*args, **kwargs)) instead of defer.execute(f, args, kwargs) with the same results.

  • 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-16T22:59:58+00:00Added an answer on May 16, 2026 at 10:59 pm

    defer.execute does indeed execute the function in a blocking manner, in the same thread and you are correct in that defer.execute(f, args, kwargs) does the same as defer.succeed(f(*args, **kwargs)) except that defer.execute will return a callback that has had the errback fired if function f throws an exception. Meanwhile, in your defer.succeed example, if the function threw an exception, it would propagate outwards, which may not be desired.

    For ease of understanding, I’ll just paste the source of defer.execute here:

    def execute(callable, *args, **kw):
        """Create a deferred from a callable and arguments.
    
        Call the given function with the given arguments.  Return a deferred which
        has been fired with its callback as the result of that invocation or its
        errback with a Failure for the exception thrown.
        """
        try:
            result = callable(*args, **kw)
        except:
            return fail()
        else:
            return succeed(result)
    

    In other words, defer.execute is just a shortcut to take a blocking function’s result as a deferred which you can then add callbacks/errbacks to. The callbacks will be fired with normal chaining semantics. It seems a bit crazy, but Deferreds can ‘fire’ before you add callbacks and the callbacks will still be called.


    So to answer your question, why is this useful? Well, defer.execute is useful both for testing / mocking as well as simply integrating an async api with synchronous code.

    Also useful is defer.maybeDeferred which calls the function and then if the function already returns a deferred simply returns it, else functions similar to defer.execute. This is useful for when you write an API which expects a callable that when called gives you a deferred, and you want to be able to accept normal blocking functions as well.

    For example, say you had an application which fetched pages and did things with it. And, for some reason, you needed to run this in a synchronous fashion for a specific use case, like in a single-shot crontab script, or in response to a request in a WSGI application, but still keep the same codebase. If your code looked like this, it could be done:

    from twisted.internet import defer
    from twisted.web.client import getPage
    
    def process_feed(url, getter=getPage):
        d = defer.maybeDeferred(getter, url)
        d.addCallback(_process_feed)
    
    def _process_feed(result):
        pass # do something with result here
    

    To run this in a synchronous context, without the reactor, you could just pass an alternate getter function, like so:

    from urllib2 import urlopen
    
    def synchronous_getter(url):
        resp = urlopen(url)
        result = resp.read()
        resp.close()
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Difference between Map and Properties as both have key-value pair.
Difference between the two functions: (function($){ console.log($); }(test)); (function($){ console.log($); })(test); I've tried it
//Difference between 2 dates This function works well but display wrong time format. Pls
I want to know difference between recursive function and using a stack in terms
Any difference between these two statements? $(document).ready(function() { // Code }); $(function() { //
Is there any difference between int on_exit(void (*function)(int , void *), void *arg); and
What is the difference between these two types of running a JavaScript function? (function()
What is the difference between following forms of function definition: $.myFunction = function() {
Difference between ADF initContext and prepareModel,since both preparing data by executing buisness service making
Difference between start-pointers and interior-pointers and in what situation we should prefer one over

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.