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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:27:51+00:00 2026-05-15T12:27:51+00:00

I have a python app that uses multiple threads and I am curious about

  • 0

I have a python app that uses multiple threads and I am curious about the best way to wait for something in python without burning cpu or locking the GIL.

my app uses twisted and I spawn a thread to run a long operation so I do not stomp on the reactor thread. This long operation also spawns some threads using twisted’s deferToThread to do something else, and the original thread wants to wait for the results from the defereds.

What I have been doing is this

while self._waiting:
    time.sleep( 0.01 )

which seemed to disrupt twisted PB’s objects from receiving messages so I thought sleep was locking the GIL. Further investigation by the posters below revealed however that it does not.

There are better ways to wait on threads without blocking the reactor thread or python posted below.

  • 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-15T12:27:51+00:00Added an answer on May 15, 2026 at 12:27 pm

    If you’re already using Twisted, you should never need to “wait” like this.

    As you’ve described it:

    I spawn a thread to run a long operation … This long operation also spawns some threads using twisted’s deferToThread …

    That implies that you’re calling deferToThread from your “long operation” thread, not from your main thread (the one where reactor.run() is running). As Jean-Paul Calderone already noted in a comment, you can only call Twisted APIs (such as deferToThread) from the main reactor thread.

    The lock-up that you’re seeing is a common symptom of not following this rule. It has nothing to do with the GIL, and everything to do with the fact that you have put Twisted’s reactor into a broken state.

    Based on your loose description of your program, I’ve tried to write a sample program that does what you’re talking about based entirely on Twisted APIs, spawning all threads via Twisted and controlling them all from the main reactor thread.

    import time
    
    from twisted.internet import reactor
    from twisted.internet.defer import gatherResults
    from twisted.internet.threads import deferToThread, blockingCallFromThread
    
    def workReallyHard():
        "'Work' function, invoked in a thread."
        time.sleep(0.2)
    
    def longOperation():
        for x in range(10):
            workReallyHard()
            blockingCallFromThread(reactor, startShortOperation, x)
        result = blockingCallFromThread(reactor, gatherResults, shortOperations)
        return 'hooray', result
    
    def shortOperation(value):
        workReallyHard()
        return value * 100
    
    shortOperations = []
    
    def startShortOperation(value):
        def done(result):
            print 'Short operation complete!', result
            return result
        shortOperations.append(
            deferToThread(shortOperation, value).addCallback(done))
    
    d = deferToThread(longOperation)
    def allDone(result):
        print 'Long operation complete!', result
        reactor.stop()
    d.addCallback(allDone)
    
    reactor.run()
    

    Note that at the point in allDone where the reactor is stopped, you could fire off another “long operation” and have it start the process all over again.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Before the dialog closes, pass this "path" back to the… May 15, 2026 at 2:43 pm
  • Editorial Team
    Editorial Team added an answer Before Inserting the Row You might Check where entries with… May 15, 2026 at 2:43 pm
  • Editorial Team
    Editorial Team added an answer I think the quickest way to do this is with… May 15, 2026 at 2:43 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.