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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:41:19+00:00 2026-05-17T00:41:19+00:00

I have a client connected to a server using twisted. The client has a

  • 0

I have a client connected to a server using twisted. The client has a thread which might potentially be doing things in the background. When the reactor is shutting down, I have to:

1) check if the thread is doing things
2) stop it if it is

What’s an elegant way to do this? The best I can do is some confused thing like:

def cleanup(self):
    isWorkingDF = defer.Deferred()
    doneDF = defer.Deferred()

    def checkIsWorking():
        res = self.stuff.isWorking() #blocking call
        reactor.callFromThread(isWorkingDF.callback, res)

    def shutdownOrNot(isWorking):
        if isWorking:
            #shutdown necessary, shutdown is also a blocking call
            def shutdown():
                self.stuff.shutdown()
                reactor.callFromThread(doneDF, None)
            reactor.callInThread(shutdown)                
        else:
            doneDF.callback(None) #no shutdown needed

    isWorkingDF.addCallback(shutdownOrNot)

    reactor.callInThread(checkIsWorking)

    return doneDF

First we check if it’s working at all. The result of that callback goes into rescallback which either shuts down or doesn’t, and then fires the doneDF, which twisted waits for until closing.

Pretty messed up eh! Is there a better way?

Maybe a related question is, is there a more elegant way to chain callbacks to each other? I could see myself needing to do more cleanup code after this is done, so then I’d have to make a different done deferred, and have the current doneDF fire a callback which does stuff then calls that done deferred..

  • 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-17T00:41:19+00:00Added an answer on May 17, 2026 at 12:41 am

    Ah the real answer is to use the defer.inlineCallbacks decorator. The above code now becomes:

    @defer.inlineCallbacks
    def procShutdownStuff(self):
        isWorking = yield deferToThread(self.stuff.isWorking)
    
        if isWorking:
            yield deferToThread(self.stuff.shutdown)
    
    def cleanup(self):
        return self.procShutdownStuff()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have program which has servers interacting with each other using Twisted's remote procedure
I have a server with 2 connections SOCKET which is connected with clients and
I have a DDE client app which connects to the DDE server app by
I have a server class which connects a client on a specific server socket
I have a client-server architecture where there is a client connected to a Central
Lets say i have an client application on iOS which is connected to a
I'm using RMI and I have a server object and many clients that connected
I have a Java client that connects to a C++ server using TCP Sockets
I have successfully built a thin client/server using Qt's Tcp Sockets API. I know
I have a client application (WPF, C#, .net4) which uses POCO entity model connected

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.