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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:54:14+00:00 2026-05-13T13:54:14+00:00

I am having troubles with the multiprocessing module. I am using a Pool of

  • 0

I am having troubles with the multiprocessing module. I am using a Pool of workers with its map method to concurrently analyze lots of files. Each time a file has been processed I would like to have a counter updated so that I can keep track of how many files remains to be processed. Here is sample code:

import os
import multiprocessing

counter = 0


def analyze(file):
    # Analyze the file.
    global counter
    counter += 1
    print counter


if __name__ == '__main__':
    files = os.listdir('/some/directory')
    pool = multiprocessing.Pool(4)
    pool.map(analyze, files)

I cannot find a solution for 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-13T13:54:15+00:00Added an answer on May 13, 2026 at 1:54 pm

    The problem is that the counter variable is not shared between your processes: each separate process is creating it’s own local instance and incrementing that.

    See this section of the documentation for some techniques you can employ to share state between your processes. In your case you might want to share a Value instance between your workers

    Here’s a working version of your example (with some dummy input data). Note it uses global values which I would really try to avoid in practice:

    from multiprocessing import Pool, Value
    from time import sleep
    
    counter = None
    
    def init(args):
        ''' store the counter for later use '''
        global counter
        counter = args
    
    def analyze_data(args):
        ''' increment the global counter, do something with the input '''
        global counter
        # += operation is not atomic, so we need to get a lock:
        with counter.get_lock():
            counter.value += 1
        print counter.value
        return args * 10
    
    if __name__ == '__main__':
        #inputs = os.listdir(some_directory)
    
        #
        # initialize a cross-process counter and the input lists
        #
        counter = Value('i', 0)
        inputs = [1, 2, 3, 4]
    
        #
        # create the pool of workers, ensuring each one receives the counter 
        # as it starts. 
        #
        p = Pool(initializer = init, initargs = (counter, ))
        i = p.map_async(analyze_data, inputs, chunksize = 1)
        i.wait()
        print i.get()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 300k
  • Answers 300k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Let me list the benefits of strong naming your assembly… May 13, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer You should be able to access the Customer directly in… May 13, 2026 at 8:04 pm
  • Editorial Team
    Editorial Team added an answer You should look into Code Contracts. The static checker is… May 13, 2026 at 8:04 pm

Related Questions

I am having trouble with the Python multiprocessing module. I am using the Process
I am having troubles with jQuery's load function and am hoping for some help.
For some reason I am having troubles with a DBI handle. Basically what happened
I'm trying to setup some caching on my site and am having troubles with
Am I correct in assuming that the only difference between "windows files" and "unix

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.