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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:59:48+00:00 2026-05-15T23:59:48+00:00

I have an existing program that has its own main loop, and does computations

  • 0

I have an existing program that has its own main loop, and does computations based on input it receives – let’s say from the user, to make it simple. I want to now do the computations remotely instead of locally, and I decided to implement the RPCs in Twisted.

Ideally I just want to change one of my functions, say doComputation(), to make a call to twisted to perform the RPC, get the results, and return. The rest of the program should stay the same. How can I accomplish this, though? Twisted hijacks the main loop when I call reactor.run(). I also read that you don’t really have threads in twisted, that all the tasks run in sequence, so it seems I can’t just create a LoopingCall and run my main loop that way.

  • 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-15T23:59:49+00:00Added an answer on May 15, 2026 at 11:59 pm

    You have a couple of different options, depending on what sort of main loop your existing program has.

    If it’s a mainloop from a GUI library, Twisted may already have support for it. In that case, you can just go ahead and use it.

    You could also write your own reactor. There isn’t a lot of great documentation for this, but you can look at the way that qtreactor implements a reactor plugin externally to Twisted.

    You can also write a minimal reactor using threadedselectreactor. The documentation for this is also sparse, but the wxpython reactor is implemented using it. Personally I wouldn’t recommend this approach as it is difficult to test and may result in confusing race conditions, but it does have the advantage of letting you leverage almost all of Twisted’s default networking code with only a thin layer of wrapping.

    If you are really sure that you don’t want your doComputation to be asynchronous, and you want your program to block while waiting for Twisted to answer, do the following:

    • start Twisted in another thread before your main loop starts up, with something like twistedThread = Thread(target=reactor.run); twistedThread.start()
    • instantiate an object to do your RPC communication (let’s say, RPCDoer) in your own main loop’s thread, so that you have a reference to it. Make sure to actually kick off its Twisted logic with reactor.callFromThread so you don’t need to wrap all of its Twisted API calls.
    • Implement RPCDoer.doRPC to return a Deferred, using only Twisted API calls (i.e. don’t call into your existing application code, so you don’t need to worry about thread safety for your application objects; pass doRPC all the information that it needs as arguments).
    • You can now implement doComputation like this:

      def doComputation(self):
          rpcResult = blockingCallFromThread(reactor, self.myRPCDoer.doRPC)
          return self.computeSomethingFrom(rpcResult)
      
    • Remember to call reactor.callFromThread(reactor.stop); twistedThread.join() from your main-loop’s shutdown procedure, otherwise you may see some confusing tracebacks or log messages on exit.

    Finally, one option that you should really consider, especially in the long term: dump your existing main loop, and figure out a way to just use Twisted’s. In my experience this is the right answer for 9 out of 10 askers of questions like this. I’m not saying that this is always the way to go – there are plenty of cases where you really need to keep your own main loop, or where it’s just way too much effort to get rid of the existing loop. But, maintaining your own loop is work too. Keep in mind that the Twisted loop has been extensively tested by millions of users and used in a huge variety of environments. If your loop is also extremely mature, that may not be a big deal, but if you’re writing a small, new program, the difference in reliability may be significant.

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

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • 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 Since version 5.7 TCPDF ( http://www.tcpdf.org ) includes full support… May 16, 2026 at 4:40 am
  • Editorial Team
    Editorial Team added an answer may be you should consider to use SQLite to store… May 16, 2026 at 4:40 am
  • Editorial Team
    Editorial Team added an answer You can't inherit an enum from another enum, although you… May 16, 2026 at 4:40 am

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.