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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:24:52+00:00 2026-05-26T22:24:52+00:00

I am writing a python script that will parse through a file quickly by

  • 0

I am writing a python script that will parse through a file quickly by sending lines to different processes to handle. At the end, I want the parent to receive the results from each child process and then be able to manipulate that. Here is the code:

#!/usr/bin/env python
import os
import re

from datetime import datetime
from multiprocessing import Process, JoinableQueue

class LineConsumer(Process):

    def __init__(self, queue):
        self.queue = queue
        self.lines = 0
        super(LineConsumer, self).__init__( )

    def run(self):
        print "My PID is %d" % self.pid

        while True:
            line = self.queue.get( )
            print self.lines
            if ':' in line:
                self.lines += 1
            self.queue.task_done( )

class Parser(object):

    def __init__(self, filename, processes=4):
        self.filename = filename
        self.processes = processes

    def parse(self):
        queue = JoinableQueue(100)
        consumers = [ ]
        parents = [ ]
        for i in range(0, self.processes):
            lc = LineConsumer(queue)
            lc.start( )
            consumers.append(lc)

        starttime = datetime.now( )
        problem = False
        numlines = 0

        with open(self.filename, 'r') as data:
            for line in data:
                numlines += 1

                def checkAlive(p):
                    if not p.is_alive( ):
                        return False
                    return True

                alive = map(checkAlive, consumers)
                if False in alive:
                    problem = True
                    print "A process died!!!"
                    break
                queue.put(line)

        if not problem:
            queue.join( )

        for p in consumers:
            print p.lines( )
            p.terminate( )
            p.join( )

        endtime = datetime.now( )
        timedelta = endtime - starttime 
        lps = numlines / timedelta.total_seconds( )
        print "Processed packets at %f lps" % lps

if __name__ == "__main__":
    import sys

    if len(sys.argv) != 2:
        print "Supply a file to read"
        sys.exit(1)

    parser = Parser(sys.argv[1])
    parser.parse( )

Here are the results:

My PID is 11578
My PID is 11579
My PID is 11580
My PID is 11581
0
1
0
2
1
3
2
1
...
555
627
564
556
628
0
0
0
0
Processed packets at 27189.771341 lps

As you can see, each child can save its line count, but when I try to access the count from the parent, I keep getting 0. How can I send the line count to the parent?

  • 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-26T22:24:53+00:00Added an answer on May 26, 2026 at 10:24 pm

    You can pass values back through an results queue.

    In LineConsumer:

    def __init__(self, queue, result_queue):
       self.result_queue = result_queue
       # ...
    
    def terminate(self):
       self.results_queue.put(self.lines)
       super(LineConsumer, self).terminate()
    

    In Parser:

    queue = JoinableQueue(100)
    result_queue = Queue()
    # ...
      lc = LineConsumer(queue, result_queue)
    # ...
    for p in consumers:
      p.terminate()
      p.join()
    
    while True:
      try:
        print results.queue.get(False)
      except Queue.Empty: # need to import Queue
        break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I amm writing a little python script that will grab information from VMs of
I'm writing a python CGI script that will query a MySQL database. I'm using
I am writing a python script that will be doing some processing on text
I am currently writing a Python script for use on Linux machines that will
I'm writing a python script that takes a file one at a time or
I'm writing a script in Python that will allow the user to input a
I'm writing a script in Python that will spit out some data organized as
I'm writing a python script that executes a csh script in Solaris 10. The
I'm writing a cross-platform python script that needs to know if and where Cygwin
I'm writing a Python backup script and I need to find the oldest file

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.