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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:54:27+00:00 2026-06-12T09:54:27+00:00

I have a simple example script constructed that defines three separate processes using multiprocessing

  • 0

I have a simple example script constructed that defines three separate processes using multiprocessing in python. My objective is to have one parent thread that spawns two smaller threads that will collect and process data.

Currently, my implementation looks like this:

from Queue import Queue,Empty
from multiprocessing import Process
import time
import hashlib


class FillQueue(Process):
    def __init__(self,q):
        Process.__init__(self)
        self.q = q

    def run(self):
        i = 0
        while i is not 5:
            print 'putting'
            self.q.put('foo')
            i+=1
        self.q.put('|STOP|')

class ConsumeQueue(Process):
    def __init__(self,q):
        Process.__init__(self)
        self.q = q

    def run(self):
        print 'Consume'
        while True:
            try:
                value =  self.q.get(False)
                print value
                if value == '|STOP|':
                    print 'done'
                    break;
            except Empty:
                print 'Nothing to process atm'

class Ripper(Process):

    q = Queue()

    def __init__(self):
        self.fq = FillQueue(self.q)
        self.cq = ConsumeQueue(self.q)
        self.fq.daemon = True
        self.cq.daemon = True

    def run(self):
        try:
            self.fq.start()
            self.cq.start()
        except KeyboardInterrupt:
            print 'exit'

if __name__ == '__main__':
    r = Ripper()
    r.start()

As it runs presently, the output from the script on CLI looks like this:

putting
putting
putting
putting
putting
Consume
foo
foo
foo
foo
foo
|STOP|
done

Obviously, the way I am starting my two threads is blocking, since the consumer doesn’t even begin to process the items in the queue until the filler finishes adding items.

How should I rewrite this to make both threads begin immediately and not block, so the consumer will simply pass to the Empty except block while there is no work to process, but will exit completely when it receives the stop message?

EDIT: typo, had the start and run methods mixed up

  • 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-12T09:54:28+00:00Added an answer on June 12, 2026 at 9:54 am

    try this:

    from Queue import Empty
    from multiprocessing import Process, Queue
    import time
    import hashlib
    
    
    class FillQueue(object):
        def __init__(self, q): 
            self.q = q 
    
        def run(self):
            i = 0 
            while i < 5:
                print 'putting'
                self.q.put('foo %d' % i ) 
                i+=1
                time.sleep(.5)
            self.q.put('|STOP|')
    
    class ConsumeQueue(object):
        def __init__(self, q): 
            self.q = q 
    
        def run(self):
            while True:
                try:
                    value =  self.q.get(False)
                    print value
                    if value == '|STOP|':
                        print 'done'
                        break;
                except Empty:
                    print 'Nothing to process atm'
                    time.sleep(.2)
    
    
    if __name__ == '__main__':
        q = Queue()
        f = FillQueue(q)
        c = ConsumeQueue(q)
    
        p1 = Process(target=f.run)
        p1.start()
    
        p2 = Process(target=c.run)
        p2.start()
    
        p1.join()
        p2.join()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple example that I am using to learn structs in
I have a relatively simple example of an MVC page that is using jQuery
I have a simple script named example : #!/bin/sh echo $'${1}' Please note that
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
so I have a simple example--a fully crossed three treatment three context experiment, where
A simple example: Let's say I have one alias being sourced somewhere as: alias
I have tried using a example file using PHPExcel script to extract data from
I have a very simple js script that works for hovers, but I want
please help I have a simple example here <script type=text/javascript language=javascript> function test(callback1) {
Have been struggling all day trying to make this simple example work using socket.io.

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.