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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:41:29+00:00 2026-05-28T18:41:29+00:00

the following code (in the main thread) works well, I grep some files and

  • 0

the following code (in the main thread) works well, I grep some files and the search until the first 100 results are found (writing the results to a file), then exit:

    command = 'grep -F "%s" %s*.txt' % (search_string, DATA_PATH)

    p = Popen(['/bin/bash', '-c', command], stdout = PIPE)
    f = open(output_file, 'w+')
    num_lines = MAX_RESULTS
    while True:  
        line = p.stdout.readline()
        print num_lines
        if line != '':
            f.write(line)
        num_lines = num_lines - 1
        if num_lines == 0:
            break
        else:
            break

The very same code used into a Process subclass, always returns grep: writing output: Broken pipe in the console:

    class Search(Process):
        def __init__(self, search_id, search_string):
            self.search_id = search_id
            self.search_string = search_string  
            self.grepped = ''
            Process.__init__(self)

        def run(self):
            output_file = TMP_PATH + self.search_id

            # flag if no regex chars
            flag = '-F' if re.match(r"^[a-zA-Z0\ ]*$", self.search_string) else '-P'    

            command = 'grep %s "%s" %s*.txt' % (flag, self.search_string, DATA_PATH)

            p = Popen(['/bin/bash', '-c', command], stdout = PIPE)
            f = open(output_file, 'w+')
            num_lines = MAX_RESULTS
            while True:  
                line = p.stdout.readline()
                print num_lines
                if line != '':
                    f.write(line)
                num_lines = num_lines - 1
                if num_lines == 0:
                    break
                else:
                    break

How come? How to fix this?

  • 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-28T18:41:29+00:00Added an answer on May 28, 2026 at 6:41 pm

    I can reproduce the error message like this:

    import multiprocessing as mp
    import subprocess
    import shlex
    
    def worker():
        proc = subprocess.Popen(shlex.split('''
            /bin/bash -c "grep -P 'foo' /tmp/test.txt"
            '''), stdout = subprocess.PIPE)
        line = proc.stdout.readline()
        print(line)
        # proc.terminate()   # This fixes the problem
    
    if __name__=='__main__':
        N = 6000
        with open('/tmp/test.txt', 'w') as f:
            f.write('bar foo\n'*N)   # <--- Increasing this number causes grep: writing output: Broken pipe
        p = mp.Process(target = worker)
        p.start()
        p.join()
    

    If the above code does not produce the error for you, increase the size of the file /tmp/test.txt by increasing N. (Conversely, you can hide the fact that there is a bug in the code by reducing N.)

    If the worker process ends before the grep subprocess, then the grep gets a SIGPIPE telling it that its stdout has been closed. grep responds by printing

    grep: writing output: Broken pipe
    

    to stderr, once for every line it is still processing.

    The fix is to terminate the process with proc.terminate() before worker ends.

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

Sidebar

Related Questions

I have the following code: main = do putStr Test input : content <-
Consider the following code: int main() { int *p; ++((int){5}); //compile without err/warning &((int){5});
I have the following code int main() { int a=6; void *p; p=&a; p++;
Consider the following code: int main() { int i; volatile int* p = &i;
Given the following code: Hashtable main = new Hashtable(); Hashtable inner = new Hashtable();
I open a pop-up window using following code in main.html function openwindow(url) { window.open(url,
The following code public class GenericsTest2 { public static void main(String[] args) throws Exception
Considering following code public class A { public static void main(String[] args) { new
The following code does not compile: public class GenericsTest { public static void main(String[]
Just run into a tricky NSFetchedResultsController problem. The following code works fine in all

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.