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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:48:42+00:00 2026-05-31T18:48:42+00:00

I modified an example with threads and got the example with multiprocessing included below.

  • 0

I modified an example with threads and got the example with multiprocessing included below. My problem is that ProduceToQueue ran fine, however ConsumeFromQueue did not finish. Why? This is one of my first multiprocessing programs, so be patient.

from Queue import Queue
import multiprocessing
import random
import time


class ProduceToQueue(multiprocessing.Process):
    def __init__(self, queue):
        multiprocessing.Process.__init__(self)
        self.shared_queue = queue

    def run(self):
        for i in range(11, 21):
            time.sleep(random.randrange(5))
            print "%s adding %s to queue" % (self.name, i)
            self.shared_queue.put(i)

        print self.name, "finished producing values"
        print "Terminating", self.name


class ConsumeFromQueue(multiprocessing.Process):
    def __init__(self, queue):
        multiprocessing.Process.__init__(self)
        self.shared_queue = queue

    def run(self):
        value = 0
        current = 10
        for i in range(10):
            time.sleep(random.randrange(3))
            print "%s attempting to read %s..." % (self.name, current + 1)
            current = self.shared_queue.get()
            print "%s read %s" % (self.name, current)
            value += current

            print "%s retrieved values totaling: %d" % (self.name, value)
            print "Terminating", self.name


queue = Queue()
producer = ProduceToQueue(queue)
consumer = ConsumeFromQueue(queue)

producer.start()
consumer.start()

producer.join()
consumer.join()
  • 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-31T18:48:43+00:00Added an answer on May 31, 2026 at 6:48 pm

    If you’re using multiprocessing instead of threading, you should be using the Queue class from multiprocessing since it’s the one that shares properly amongst processes. See the Python docs on the multithreading module for details.

    The standard Queue class (from Queue) will effectively be split into two separate queues whenever you create a child process. That means, in your situation, you have the queue in the main process (which nobody is touching), a different queue in the producer (which is being written to) and yet another queue in the consumer (which is being read from).

    And there’s no connection whatsoever between those three.

    The quickest fix is to simply change:

    from Queue import Queue
    

    into:

    from multiprocessing import Queue
    

    so that your queues are of the correct type to work between processes rather than just threads. Or just get rid of that from line altogether and use:

    queue = multiprocessing.Queue()
    

    In addition (though I realise this is just play code), it’s very unusual to have a sleep operation in the consumer, you would usually want to just keep that ready to roll for when a item appears on the queue.

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

Sidebar

Related Questions

The example below throws an InvalidOperationException, Collection was modified; enumeration operation may not execute.
I have created a little example(below) on my attempt to safely stop multiple threads
I have a unit test (example is modified Test::Unit documentation ) require 'test/unit' class
I've slightly modified the iPhone SDK's GLSprite example while learning OpenGL ES and it
I am having an OpenGL VBO problem. I downloaded the old VBO example called
I've modified Producer/Consumer example http://msdn.microsoft.com/en-us/library/yy12yx1f(v=vs.80).aspx . I don't want Consumer to process queue on
I have a collection of objects that is modified by one thread and read
I have this modified code example I took from the wikipedia article on anonymous
Suppose I have this code executed by two different threads T1 and T2 that
EDIT: Modified title and added update. UPDATE : We no longer believe this is

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.