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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:36:53+00:00 2026-05-20T10:36:53+00:00

The Python code below connects to lots of servers, grabs some info from each

  • 0

The Python code below connects to lots of servers, grabs some info from each one and returns the results. It currently kicks off a separate thread for each connection. I would like to see how performance is affected by using a separate process for each connection rather than a thread. Can this code be easily changed to use processes instead of threads? What exactly would I need to do? What are the risks, if any?

Python 2.6 / Platform Linux

class ServerInfoGetter(threading.Thread):

    def __init__(self, host, port=DEFAULT_PORT, timeout=15):
        self.host = host
        self.timeout = timeout
        self.port = port
        self.result = None
        threading.Thread.__init__(self)

    def get_result(self):
        return self.result

    def run(self):
        try:
            serv_check = ServCheck(self.host, \
                                    port=self.port, \
                                    timeout=self.timeout)
            serv_check.get_info()
            self.result = serv_check
        except Exception, err:
            logging.debug("Could not run ServCheck for : %s %s",self.host, err)


def process_hosts(hosts_and_ports):

    def producer(queue, hosts_and_ports):
        for host, ports in hosts_and_ports.items():
            for  port in ports:
                logging.info("processing host: %s:%s", host, port)
                thread = ServerInfoGetter(str(host), port)
                thread.start()
                queue.put(thread, True) # True so block until slot available

    results = []

    def consumer(queue, total_checks):
        while len(results) < total_checks:
            thread = queue.get(True)
            thread.join()
            results.append(thread.get_result())

    logging.info("processing hosts")
    queue = Queue(QUEUE_SIZE)
    prod_thread = threading.Thread(target=producer,
                                    args=(queue,
                                    hosts_and_ports))

    cons_thread = threading.Thread(target=consumer,
                                    args=(queue,
                                    calculate_total_checks(hosts_and_ports)))
    prod_thread.start()
    cons_thread.start()
    prod_thread.join()
    cons_thread.join()
    return results
  • 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-20T10:36:54+00:00Added an answer on May 20, 2026 at 10:36 am

    As it says in the documentation:

    multiprocessing is a package that supports spawning processes using an API similar to the threading module. […] In multiprocessing, processes are spawned by creating a Process object and then calling its start() method. Process follows the API of threading.Thread.

    So, basically, you just have to replace all threading.Thread objects with multiprocessing.Process objects (and similarly, the queue needs to be replaced with a multiprocessing.Queue object).

    At least, that’s how it would appear. However, in practice, all objects that need to cross Process boundaries need to be multiprocessing.Value objects. Otherwise, they will never update across threads.

    This includes self.host, self.timeout, self.port, self.result if you’re only going to modify the ServerInfoGetter class. Read the rest of the multiprocessing doc to get an idea for the other data types that you’ll need to use.

    Also, as a sidenote, I’m not sure if it would be a problem for python 2.6 on Linux, but for python 2.7 on Windows, both idle and the interactive interpreter have trouble (for me, at least) with multiprocessing. These problems go away when directly executing the script with the python or pythonw executables. Update – python 2.5.1 on my Slackware box doesn’t have this problem, so you may be fine in interactive mode as well… although winwaed wasn’t, so who knows…?

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

Sidebar

Related Questions

I'm looking for Python code that removes C and C++ comments from a string.
I have some Python code that works correctly when I use python.exe to run
I have some old python code that uses the pywin32 extensions. Starting out with
I have a method in my Python code that returns a tuple - a
I am interested in getting some Python code talking to some Ruby code on
I'm trying to run some python code under Apache 2.2 / mod_python 3.2.8. Eventually
I have python code below that will loop through a table and print out
In some Python code, I fork and do some processing in a child process
If I have Python code class A(): pass class B(): pass class C(A, B):
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)

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.