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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:37:40+00:00 2026-06-08T00:37:40+00:00

I’m having much trouble trying to understand just how the multiprocessing queue works on

  • 0

I’m having much trouble trying to understand just how the multiprocessing queue works on python and how to implement it. Lets say I have two python modules that access data from a shared file, let’s call these two modules a writer and a reader. My plan is to have both the reader and writer put requests into two separate multiprocessing queues, and then have a third process pop these requests in a loop and execute as such.

My main problem is that I really don’t know how to implement multiprocessing.queue correctly, you cannot really instantiate the object for each process since they will be separate queues, how do you make sure that all processes relate to a shared queue (or in this case, queues)

  • 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-08T00:37:41+00:00Added an answer on June 8, 2026 at 12:37 am

    Short Summary

    As of CY2023, the technique described in this answer is quite out of date. These days, use concurrent.futures.ProcessPoolExecutor() instead of multiprocessing, below…

    This answer describes the benefits and shortcomings of using concurrent.futures.ProcessPoolExecutor(). FYI, multiple python processes are sometimes used instead of threading to get the most benefit from concurrency. That said, python threading works pretty well as long as there is sufficient CPU activity to avoid the GIL (activity such as sending / receiving network traffic).

    Original Answer

    My main problem is that I really don’t know how to implement multiprocessing.queue correctly, you cannot really instantiate the object for each process since they will be separate queues, how do you make sure that all processes relate to a shared queue (or in this case, queues)

    This is a simple example of a reader and writer sharing a single queue… The writer sends a bunch of integers to the reader; when the writer runs out of numbers, it sends ‘DONE’, which lets the reader know to break out of the read loop.

    You can spawn as many reader processes as you like…

    from multiprocessing import Process, Queue
    import time
    import sys
    
    
    def reader_proc(queue):
        """Read from the queue; this spawns as a separate Process"""
        while True:
            msg = queue.get()  # Read from the queue and do nothing
            if msg == "DONE":
                break
    
    
    def writer(count, num_of_reader_procs, queue):
        """Write integers into the queue.  A reader_proc() will read them from the queue"""
        for ii in range(0, count):
            queue.put(ii)  # Put 'count' numbers into queue
    
        ### Tell all readers to stop...
        for ii in range(0, num_of_reader_procs):
            queue.put("DONE")
    
    
    def start_reader_procs(qq, num_of_reader_procs):
        """Start the reader processes and return all in a list to the caller"""
        all_reader_procs = list()
        for ii in range(0, num_of_reader_procs):
            ### reader_p() reads from qq as a separate process...
            ###    you can spawn as many reader_p() as you like
            ###    however, there is usually a point of diminishing returns
            reader_p = Process(target=reader_proc, args=((qq),))
            reader_p.daemon = True
            reader_p.start()  # Launch reader_p() as another proc
    
            all_reader_procs.append(reader_p)
    
        return all_reader_procs
    
    
    if __name__ == "__main__":
        num_of_reader_procs = 2
        qq = Queue()  # writer() writes to qq from _this_ process
        for count in [10**4, 10**5, 10**6]:
            assert 0 < num_of_reader_procs < 4
            all_reader_procs = start_reader_procs(qq, num_of_reader_procs)
    
            writer(count, len(all_reader_procs), qq)  # Queue stuff to all reader_p()
            print("All reader processes are pulling numbers from the queue...")
    
            _start = time.time()
            for idx, a_reader_proc in enumerate(all_reader_procs):
                print("    Waiting for reader_p.join() index %s" % idx)
                a_reader_proc.join()  # Wait for a_reader_proc() to finish
    
                print("        reader_p() idx:%s is done" % idx)
    
            print(
                "Sending {0} integers through Queue() took {1} seconds".format(
                    count, (time.time() - _start)
                )
            )
            print("")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't

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.