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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:34:03+00:00 2026-05-14T02:34:03+00:00

I unsuccessfully tried using txredis (the non blocking twisted api for redis) for a

  • 0

I unsuccessfully tried using txredis (the non blocking twisted api for redis) for a persisting message queue I’m trying to set up with a scrapy project I am working on. I found that although the client was not blocking, it became much slower than it could have been because what should have been one event in the reactor loop was split up into thousands of steps.

So instead, I tried making use of redis-py (the regular blocking twisted api) and wrapping the call in a deferred thread. It works great, however I want to perform an inner deferred when I make a call to redis as I would like to set up connection pooling in attempts to speed things up further.

Below is my interpretation of some sample code taken from the twisted docs for a deferred thread to illustrate my use case:

#!/usr/bin/env python
from twisted.internet import reactor,threads
from twisted.internet.task import LoopingCall
import time

def main_loop():
    print 'doing stuff in main loop.. do not block me!'


def aBlockingRedisCall():
    print 'doing lookup... this may take a while'
    time.sleep(10)
    return 'results from redis'

def result(res):
    print res

def main():
    lc = LoopingCall(main_loop)
    lc.start(2)
    d = threads.deferToThread(aBlockingRedisCall)
    d.addCallback(result)
    reactor.run()

if __name__=='__main__':
    main()

And here is my alteration for connection pooling that makes the code in the deferred thread blocking :

#!/usr/bin/env python
from twisted.internet import reactor,defer
from twisted.internet.task import LoopingCall
import time

def main_loop():
    print 'doing stuff in main loop.. do not block me!'

def aBlockingRedisCall(x):
    if x<5: #all connections are busy, try later
        print '%s is less than 5, get a redis client later' % x
        x+=1
        d = defer.Deferred()
        d.addCallback(aBlockingRedisCall)
        reactor.callLater(1.0,d.callback,x)
        return d

    else: 
        print 'got a redis client; doing lookup.. this may take a while'
        time.sleep(10) # this is now blocking.. any ideas?
        d = defer.Deferred()
        d.addCallback(gotFinalResult)
        d.callback(x)
        return d

def gotFinalResult(x):
    return 'final result is %s' % x

def result(res):
    print res

def aBlockingMethod():
    print 'going to sleep...'
    time.sleep(10)
    print 'woke up'

def main():
    lc = LoopingCall(main_loop)
    lc.start(2)


    d = defer.Deferred()
    d.addCallback(aBlockingRedisCall)
    d.addCallback(result)
    reactor.callInThread(d.callback, 1)
    reactor.run()

if __name__=='__main__':
    main()

So my question is, does anyone know why my alteration causes the deferred thread to be blocking and/or can anyone suggest a better solution?

  • 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-14T02:34:04+00:00Added an answer on May 14, 2026 at 2:34 am

    Well, as the twisted docs say:

    Deferreds do not make the code
    magically not block

    Whenever you’re using blocking code, such as sleep, you have to defer it to a new thread.

    #!/usr/bin/env python
    from twisted.internet import reactor,defer, threads
    from twisted.internet.task import LoopingCall
    import time
    
    def main_loop():
        print 'doing stuff in main loop.. do not block me!'
    
    def aBlockingRedisCall(x):
        if x<5: #all connections are busy, try later
            print '%s is less than 5, get a redis client later' % x
            x+=1
            d = defer.Deferred()
            d.addCallback(aBlockingRedisCall)
            reactor.callLater(1.0,d.callback,x)
            return d
    
        else: 
            print 'got a redis client; doing lookup.. this may take a while'
            def getstuff( x ):
                time.sleep(3)
                return "stuff is %s" % x
    
            # getstuff is blocking, so you need to push it to a new thread
            d = threads.deferToThread(getstuff, x)
            d.addCallback(gotFinalResult)
            return d
    
    def gotFinalResult(x):
        return 'final result is %s' % x
    
    def result(res):
        print res
    
    def aBlockingMethod():
        print 'going to sleep...'
        time.sleep(10)
        print 'woke up'
    
    def main():
        lc = LoopingCall(main_loop)
        lc.start(2)
    
    
        d = defer.Deferred()
        d.addCallback(aBlockingRedisCall)
        d.addCallback(result)
        reactor.callInThread(d.callback, 1)
        reactor.run()
    
    if __name__=='__main__':
        main()
    

    In case the redis api is not very complex it might be more natural to rewrite it using twisted.web, instead of just calling the blocking api in a lot threads.

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

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you able to configure proxy settings on the embedded… May 14, 2026 at 8:11 pm
  • Editorial Team
    Editorial Team added an answer Alright, I got it. I wasn't properly declaring my validator.… May 14, 2026 at 8:11 pm
  • Editorial Team
    Editorial Team added an answer The way I solve this for paginating results from a… May 14, 2026 at 8:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.