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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:42:23+00:00 2026-06-09T05:42:23+00:00

I am experimenting multiprocessing in Python and tried to share an Array of strings

  • 0

I am experimenting multiprocessing in Python and tried to share an Array of strings among two processes. Here is my python code :

from multiprocessing import Process, Array, Value
import ctypes

def f1(a, v):
    for i, l in enumerate(['a', 'b', 'c']):
        a[i] = l*3

    v.value += 1

    print "f1 : ", a[:], v.value

def f2(a,v):

    v.value += 1

    print "f2 : ", a[:], v.value

if __name__ == '__main__':
    val = Value(ctypes.c_int, 0)
    arr = Array(ctypes.c_char_p, 3)

    print "Before :", arr[:], val.value

    p = Process(target=f1, args=(arr, val))
    p2 = Process(target=f2, args=(arr, val))

    p.start()
    p2.start()

    p.join()
    p2.join()

    print "After : ", arr[:], val.value

When I run the script I see that arr is correctly populated and available in f1() but not in f2(). Here is the result:

    % python /tmp/tests.py
    Before : [None, None, None] 0
    f1 :  ['aaa', 'bbb', 'ccc'] 1
    f2 :  ['\x01', '\x11', '\x01'] 2
    After :  ['\x01', '\x01', '\x01'] 2

Did I overlooked something ?

Thanks in advance for your feedback. 🙂

  • 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-09T05:42:25+00:00Added an answer on June 9, 2026 at 5:42 am

    My guess is:

    arr stores 3 pointers. f1() assigns them to memory addresses that have no
    meaning outside current process. f2() tries to access the
    meaningless addresses that contain junk at this point.

    Assigning to values that have meaning in all processes seems to help:

    from __future__ import print_function
    import ctypes
    import time
    from multiprocessing import Process, Array, Value
    
    values = [(s*4).encode('ascii') for s in 'abc']
    
    def f1(a, v):
        for i, s in enumerate(values):
            a[i] = s
    
        v.value += 1
    
        print("f1 : ", a[:], v.value)
    
    def f2(a,v):
        v.value += 1
        print("f2 : ", a[:], v.value)
    
    def main():
        val = Value(ctypes.c_int, 0)
        arr = Array(ctypes.c_char_p, 3)
    
        print("Before :", arr[:], val.value)
    
        p = Process(target=f1, args=(arr, val))
        p2 = Process(target=f2, args=(arr, val))
    
        p.start()
        p2.start()
    
        p.join()
        p2.join()
    
        print("After : ", arr[:], val.value)
    
    if __name__ == '__main__':
        main()
    

    Output

    Before : [None, None, None] 0
    f1 :  ['aaaa', 'bbbb', 'cccc'] 1
    f2 :  ['aaaa', 'bbbb', 'cccc'] 2
    After :  ['aaaa', 'bbbb', 'cccc'] 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While experimenting a bit with C++ templates I managed to produce this simple code,
I am experimenting with following code: http://www.jsresources.org/examples/MidiNote.java.html to operate my Novation Launchpad MIDI Controller.
Experimenting with new features of T-SQL, I've run into a puzzle. Here is some
I've been experimenting with the standard python math module and have come across some
Experimenting with a battery monitor icon at the moment in Python using pygtk and
In experimenting with pickle, I've put together some code for a (very) simple blog.
I was experimenting with Java reflection and inlined Strings and came up with the
Im experimenting with the following code private void timer1_Tick(object sender, EventArgs e) { Thread
Experimenting using different approaches and reducing code maintenance I have ended up using reflection
After experimenting with an iterator block I noticed the generated IL code is not

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.