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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:37:39+00:00 2026-05-23T21:37:39+00:00

I have a Python program that uses Popen to call a test C++ program.

  • 0

I have a Python program that uses Popen to call a test C++ program. The test C++ program simply writes 0-99999 to stdout. The Python program has two functions that should be run as seperate processes. One function, funcA, should launch the C++ program, read the integers from the stdout pipe, and insert those integers into a shared Queue. The other function, funcB, should read and print the integers in the Queue until the Queue is empty. I have some issues / questions that I will post below, along with my code below that.

  1. What is the proper way for funcA to read from the C++ program’s stdout until it (the C++ program) terminates?
  2. What is the proper way for funcB to read from the shared Queue until all ints have been processed?

My current method for question 1 works, I believe, but I know there may be some issues that I don’t check such as the Queue filling up. Also, all the numbers are not printed out (stops at about 98000) and I think this might have something to do with funcA terminating and disrupting the shared Queue? I am not exactly sure what to do for question 2 because the documentation says that one can’t rely on empty() in a concurrent processing atmosphere and I don’t want to use a while(1).

import multiprocessing
import subprocess
import Queue

def funcA(intQueue):
    # call C++ program
    handle = subprocess.Popen(['../C++/C++.exe'], stdout=subprocess.PIPE)

    while(handle.returncode == None):
        handle.stdout.readline()
        intQueue.put(handle.stdout.readline())
        handle.poll()

def funcB(intQueue):
    try:
        while(1):
            print intQueue.get(True, 2)
    except Queue.Empty:
        pass

if __name__ == "__main__":
    # shared Queue for all the processes
    intQueue = multiprocessing.Queue()

    # producer - receives ints from the C++ stdout and inserts into Queue
    multiprocessing.Process(target=funcA, args=(intQueue,)).start()

    # consumer - prints ints from the Queue
    multiprocessing.Process(target=funcB, args=(intQueue,)).start()
  • 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-23T21:37:39+00:00Added an answer on May 23, 2026 at 9:37 pm

    In case anyone comes across this same problem:

    For question 1 I used a while(1) that breaks when the list returned from a splitting of handle.stdout.read() has a length of 1 (this means that nothing was returned from the pipe).

    For question 2 I used the poison pill method described in this post: http://www.doughellmann.com/PyMOTW/multiprocessing/communication.html

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

Sidebar

Related Questions

I have a python program that uses subprocess.Popen to launch another process (python process
Problem: I have a monitor program in Python that uses subprocess' Popen to start
I have a Python program that uses the threading module. Once every second, my
I'm writing a python script that uses subprocess.Popen to execute two programs (from compiled
I have a python program that uses a custom-built DLL. This DLL crashes due
I have a python program that imports pythoncom (and uses pythoncom.CoCreateInstance from it). I
I have a python program that has no windows frame and doesn't show up
I have a Python script that calls an executable program with various arguments (in
I have a Python script that needs to execute an external program, but for
I'd like to have a python program alert me when it has completed its

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.