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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:50:07+00:00 2026-06-01T09:50:07+00:00

I wanted to implement a simple python program using parallel execution. It’s I/O bound,

  • 0

I wanted to implement a simple python program using parallel execution. It’s I/O bound, so I figured threads would be appropriate (as opposed to processes). After reading the documentation for Queue and fork, I thought something like the following might work.

q = Queue.Queue()

if os.fork():            # child
    while True:
        print q.get()
else:                    # parent
    [q.put(x) for x in range(10)]

However, the get() call never returns. I thought it would return once the other thread executes a put() call. Using the threading module, things behave more like I expected:

q = Queue.Queue()

def consume(q):
    while True:
        print q.get()

worker = threading.Thread (target=consume, args=(q,))
worker.start()

[q.put(x) for x in range(10)]

I just don’t understand why the fork approach doesn’t do the same thing. What am I missing?

  • 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-01T09:50:09+00:00Added an answer on June 1, 2026 at 9:50 am

    The POSIX fork system call creates a new process, rather than a new thread inside the same adress space:

    The fork() function shall create a new process. The new process (child
    process) shall be an exact copy of the calling process (parent
    process) except as detailed below: […]

    So the Queue is duplicated in your first example, rather than shared between the parent and child.

    You can use multiprocessing.Queue instead or just use threads like in your second example 🙂

    By the way, using list comprehensions just for side effects isn’t good practice for several reasons. You should use a for loop instead:

    for x in range(10): q.put(x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to implement python com server using win32com extensions. Then consume the server
Wanted to write a program to implement a dictionary of words using Tries Data
I've been using rpclib to auto-generate a WSDL and implement it in Python. Then
I wanted to implement the game Pacman. For the AI, I was thinking of
If I wanted to implement an app for 2 Android devices so I can
Let's say you wanted to implement a breadth-first search of a binary tree recursively
I wanted to understand how we can implement a safe logout method in a
I wanted to implement Chrome's Omnibox search into my site, and followed the relatively
I wanted to implement the ActionBar (Android 4.0) in a test App to see
I'm working on a simple game design, and I wanted to break up my

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.