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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:24:02+00:00 2026-06-17T08:24:02+00:00

I’m launching 3 processes and I want them to put a string into a

  • 0

I’m launching 3 processes and I want them to put a string into a shared array, at the index corresponding to the process (i).

Look at the code below, the output generated is:

['test 0', None, None]
['test 1', 'test 1', None]
['test 2', 'test 2', 'test 2']

Why ‘test 0’ get overwritten by test 1, and test 1 by test 2?

What I want is (order is not important) :

['test 0', None, None]
['test 0', 'test 1', None]
['test 0', 'test 1', 'test 2']

The code :

#!/usr/bin/env python

import multiprocessing
from multiprocessing import Value, Lock, Process, Array
import ctypes
from ctypes import c_int, c_char_p

class Consumer(multiprocessing.Process):
    def __init__(self, task_queue, result_queue, arr, lock):
            multiprocessing.Process.__init__(self)
            self.task_queue = task_queue
            self.result_queue = result_queue
            self.arr = arr
            self.lock = lock

    def run(self):
            proc_name = self.name
            while True:
                next_task = self.task_queue.get()
                if next_task is None:
                    self.task_queue.task_done()
                    break            
                answer = next_task(arr=self.arr, lock=self.lock)
                self.task_queue.task_done()
                self.result_queue.put(answer)
            return

class Task(object):
    def __init__(self, i):
        self.i = i

    def __call__(self, arr=None, lock=None):
        with lock:
            arr[self.i] = "test %d" % self.i
            print arr[:]

    def __str__(self):
        return 'ARC'

    def run(self):
        print 'IN'

if __name__ == '__main__':
   tasks = multiprocessing.JoinableQueue()
   results = multiprocessing.Queue()

   arr = Array(ctypes.c_char_p, 3)

   lock = multiprocessing.Lock()

   num_consumers = multiprocessing.cpu_count() * 2
   consumers = [Consumer(tasks, results, arr, lock) for i in xrange(num_consumers)]

   for w in consumers:
      w.start()

   for i in xrange(3):
      tasks.put(Task(i))

   for i in xrange(num_consumers):
      tasks.put(None)

I’m running Python 2.7.3 (Ubuntu)

  • 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-17T08:24:03+00:00Added an answer on June 17, 2026 at 8:24 am

    This problem seems similar to this one. There, J.F. Sebastian speculated that the assignment to arr[i] points arr[i] to a memory address that was only meaningful to the subprocess making the assignment. The other subprocesses retrieve garbage when looking at that address.

    There are at least two ways to avoid this problem. One is to use a multiprocessing.manager list:

    import multiprocessing as mp
    
    class Consumer(mp.Process):
        def __init__(self, task_queue, result_queue, lock, lst):
                mp.Process.__init__(self)
                self.task_queue = task_queue
                self.result_queue = result_queue
                self.lock = lock
                self.lst = lst
    
        def run(self):
                proc_name = self.name
                while True:
                    next_task = self.task_queue.get()
                    if next_task is None:
                        self.task_queue.task_done()
                        break            
                    answer = next_task(lock = self.lock, lst = self.lst)
                    self.task_queue.task_done()
                    self.result_queue.put(answer)
                return
    
    class Task(object):
        def __init__(self, i):
            self.i = i
    
        def __call__(self, lock, lst):
            with lock:
                lst[self.i] = "test {}".format(self.i)
                print([lst[i] for i in range(3)])
    
    if __name__ == '__main__':
       tasks = mp.JoinableQueue()
       results = mp.Queue()
       manager = mp.Manager()
       lst = manager.list(['']*3)
    
       lock = mp.Lock()
       num_consumers = mp.cpu_count() * 2
       consumers = [Consumer(tasks, results, lock, lst) for i in xrange(num_consumers)]
    
       for w in consumers:
          w.start()
    
       for i in xrange(3):
          tasks.put(Task(i))
    
       for i in xrange(num_consumers):
          tasks.put(None)
    
       tasks.join()
    

    Another way is to use a shared array with a fixed size such as mp.Array('c', 10).

    import multiprocessing as mp
    
    class Consumer(mp.Process):
        def __init__(self, task_queue, result_queue, arr, lock):
                mp.Process.__init__(self)
                self.task_queue = task_queue
                self.result_queue = result_queue
                self.arr = arr
                self.lock = lock
    
        def run(self):
                proc_name = self.name
                while True:
                    next_task = self.task_queue.get()
                    if next_task is None:
                        self.task_queue.task_done()
                        break            
                    answer = next_task(arr = self.arr, lock = self.lock)
                    self.task_queue.task_done()
                    self.result_queue.put(answer)
                return
    
    class Task(object):
        def __init__(self, i):
            self.i = i
    
        def __call__(self, arr, lock):
            with lock:
                arr[self.i].value = "test {}".format(self.i)
                print([a.value for a in arr])
    
    if __name__ == '__main__':
       tasks = mp.JoinableQueue()
       results = mp.Queue()
       arr = [mp.Array('c', 10) for i in range(3)]
    
       lock = mp.Lock()
       num_consumers = mp.cpu_count() * 2
       consumers = [Consumer(tasks, results, arr, lock) for i in xrange(num_consumers)]
    
       for w in consumers:
          w.start()
    
       for i in xrange(3):
          tasks.put(Task(i))
    
       for i in xrange(num_consumers):
          tasks.put(None)
    
       tasks.join()
    

    I speculate that the reason why this works when mp.Array(ctypes.c_char_p, 3) does not, is because mp.Array('c', 10) has a fixed size so the memory address never changes, while mp.Array(ctypes.c_char_p, 3) has a variable size, so the memory address might change when arr[i] is assigned to a bigger string.

    Perhaps this is what the docs are warning about when it states,

    Although it is possible to store a pointer in shared memory remember
    that this will refer to a location in the address space of a specific
    process. However, the pointer is quite likely to be invalid in the
    context of a second process and trying to dereference the pointer from
    the second process may cause a crash.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am confused How to use looping for Json response Array in another Array.
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.