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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:40:45+00:00 2026-06-01T19:40:45+00:00

What techniques are people using to utilize multiple processors/cores when running a TwistedWeb server?

  • 0

What techniques are people using to utilize multiple processors/cores when running a TwistedWeb server? Is there a recommended way of doing it?

My twisted.web based web service is running on Amazon EC2 instances, which often have multiple CPU cores (8, 16), and the type of work that the service is doing benefits from extra processing power, so i would very much like to use that.

I understand that it is possible to use haproxy, squid or a web server, configured as a reverse proxy, in front of multiple instances of Twisted. In fact, we are currently using such a setup, with nginx serving as a reverse proxy to several upstream twisted.web services running on the same host, but each on different port.

This works fine, but what i’m really interested in, is a solution where there is no “front-facing” server, but all twistd processes somehow bind to the same socket and accept requests. Is such thing even possible… or am i being crazy? The operating system is Linux (CentOS).

Thanks.

Anton.

  • 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-06-01T19:40:47+00:00Added an answer on June 1, 2026 at 7:40 pm

    There are a number of ways to support multiprocess operation for a Twisted application. One important question to answer at the start, though, is what you expect your concurrency model to be, and how your application deals with shared state.

    In a single process Twisted application, concurrency is all cooperative (with help from Twisted’s asynchronous I/O APIs) and shared state can be kept anywhere a Python object would go. Your application code runs knowing that, until it gives up control, nothing else will run. Additionally, any part of your application that wants to access some piece of shared state can probably do so quite easily, since that state is probably kept in a boring old Python object that is easy to access.

    When you have multiple processes, even if they’re all running Twisted-based applications, then you have two forms of concurrency. One is the same as for the previous case – within a particular process, the concurrency is cooperative. However, you have a new kind, where multiple processes are running. Your platform’s process scheduler might switch execution between these processes at any time, and you have very little control over this (as well as very little visibility into when it happens). It might even schedule two of your processes to run simultaneously on different cores (this is probably even what you’re hoping for). This means that you lose some guarantees about consistency, since one process doesn’t know when a second process might come along and try to operate on some shared state. This leads in to the other important area of consideration, how you will actually share state between the processes.

    Unlike the single process model, you no longer have any convenient, easily accessed places to store your state where all your code can reach it. If you put it in one process, all the code in that process can access it easily as a normal Python object, but any code running in any of your other processes no longer has easy access to it. You might need to find an RPC system to let your processes communicate with each other. Or, you might architect your process divide so that each process only receives requests which require state stored in that process. An example of this might be a web site with sessions, where all state about a user is stored in their session, and their sessions are identified by cookies. A front-end process could receive web requests, inspect the cookie, look up which back-end process is responsible for that session, and then forward the request on to that back-end process. This scheme means that back-ends typically don’t need to communicate (as long as your web application is sufficiently simple – ie, as long as users don’t interact with each other, or operate on shared data).

    Note that in that example, a pre-forking model is not appropriate. The front-end process must exclusively own the listening port so that it can inspect all incoming requests before they are handled by a back-end process.

    Of course, there are many types of application, with many other models for managing state. Selecting the right model for multi-processing requires first understanding what kind of concurrency makes sense for your application, and how you can manage your application’s state.

    That being said, with very new versions of Twisted (unreleased as of this point), it’s quite easy to share a listening TCP port amongst multiple processes. Here is a code snippet which demonstrates one way you might use some new APIs to accomplish this:

    from os import environ
    from sys import argv, executable
    from socket import AF_INET
    
    from twisted.internet import reactor
    from twisted.web.server import Site
    from twisted.web.static import File
    
    def main(fd=None):
        root = File("/var/www")
        factory = Site(root)
    
        if fd is None:
            # Create a new listening port and several other processes to help out.                                                                     
            port = reactor.listenTCP(8080, factory)
            for i in range(3):
                reactor.spawnProcess(
                        None, executable, [executable, __file__, str(port.fileno())],
                    childFDs={0: 0, 1: 1, 2: 2, port.fileno(): port.fileno()},
                    env=environ)
        else:
            # Another process created the port, just start listening on it.                                                                            
            port = reactor.adoptStreamPort(fd, AF_INET, factory)
    
        reactor.run()
    
    
    if __name__ == '__main__':
        if len(argv) == 1:
            main()
        else:
            main(int(argv[1]))
    

    With older versions, you can sometimes get away with using fork to share the port. However, this is rather error prone, fails on some platforms, and isn’t a supported way to use Twisted:

    from os import fork
    
    from twisted.internet import reactor
    from twisted.web.server import Site
    from twisted.web.static import File
    
    def main():
        root = File("/var/www")
        factory = Site(root)
    
        # Create a new listening port
        port = reactor.listenTCP(8080, factory)
    
        # Create a few more processes to also service that port
        for i in range(3):
            if fork() == 0:
                # Proceed immediately onward in the children.
                # The parent will continue the for loop.
                break
    
        reactor.run()
    
    
    if __name__ == '__main__':
        main()
    

    This works because of the normal behavior of fork, where the newly created process (the child) inherits all of the memory and file descriptors from the original process (the parent). Since processes are otherwise isolated, the two processes don’t interfere with each other, at least as far as the Python code they are executing goes. Since the file descriptors are inherited, either the parent or any of the children can accept connections on the port.

    Since forwarding HTTP requests is such an easy task, I doubt you’ll notice much of a performance improvement using either of these techniques. The former is a bit nicer than proxying, because it simplifies your deployment and works for non-HTTP applications more easily. The latter is probably more of a liability than it’s worth accepting.

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

Sidebar

Related Questions

I see people using all these different techniques with jQuery. I know the 2nd
So I wanted to open a dialog in the community about various techniques people
What techniques do people use to consume services in the REST stile on .Net
I've seen a lot of people try to code their own image conversion techniques.
What techniques or tools are recommended for finding broken links on a website? I
In Visual Studio 2008 using C#, what is the best way to share code
I'm curious to know what usage patterns people have established for themselves to utilize
Lots of BAWs (big ass-websites) are using data storage and retrieval techniques that rely
I've been doing quite a bit of debugging of managed applications lately using both
Recently thanks to rails' popularity, many people start using activerecord as model. however, before

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.