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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:43:51+00:00 2026-06-11T01:43:51+00:00

I’ve a situation like this: The main process generate some sub-process that they should

  • 0

I’ve a situation like this:

The main process generate some sub-process that they should write the result in a shared object in string and numeric types, for the numeric types there’s no problem but with the string the value will be lost.

import multiprocessing as mp
from ctypes import Structure, c_double, c_wchar_p, c_int

# shared obj class
class SharedObj(Structure):
    _fields_ = [('name', c_wchar_p), ('val', c_double) ]

def run_mp( values , lock , s ) :
    for i in range( s , len( values ) , 2 ):
        lock.acquire()
        values[i].name = str( i ) # write the string value in the shared obj
        values[i].val = float( i )
        print( "tmp: %d" % i )
        lock.release()

def main():
    # creating the shared obj and mutex
    values = mp.Array(  SharedObj , [SharedObj() for i in range( 10 )] )
    lock_j = mp.Lock()

    # creating two sub-process form the function run_mp
    p1 = mp.Process( target=run_mp , args=( values , lock_j , 0 ))
    p2 = mp.Process( target=run_mp , args=( values , lock_j , 1 ))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    for v in  values:
        print()
        print( "res name: %s" % v.name )
        print( "res val: %f " % v.val )


if __name__ == '__main__':
    main()

As a result the field in the shared object containing the c_double is written in the field, but the string generated in the sub-processes rum-mp ( string values[i].name = str( i ) ) will be lost in the main process.

There is a method for saving the strings generated in sub-process?

The output of this code looks like:

Where the resulting string in the main process are completely random.

tmp: 0
tmp: 2
tmp: 3
tmp: 4

res name: ����羍����羍
res val: 0.000000
res name: ����羍����羍
res val: 1.000000
res name:
res val: 2.000000   ....
  • 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-11T01:43:53+00:00Added an answer on June 11, 2026 at 1:43 am

    Here is a slightly modified version of your code:

    #!/usr/bin/env python
    
    import multiprocessing as mp
    
    
    def run_mp( values ):
        for c_arr, c_double in values:
            c_arr.value = 'hello foo'
            c_double.value = 3.14
    
    def main():
        lock = mp.Lock()
        child_feed = []
        for i in range(10):
            child_feed.append((
                mp.Array('c', 15, lock = lock),
                mp.Value('d', 1.0/3.0, lock = lock)
            ))
    
        p1 = mp.Process( target=run_mp , args=(child_feed,))
        p2 = mp.Process( target=run_mp , args=(child_feed,))
    
        p1.start()
        p2.start()
    
        p1.join()
        p2.join()
    
        for c_arr, c_double in child_feed:
            print()
            print( "res name: %s" % c_arr.value )
            print( "res val: %f " % c_double.value )
    
    
    if __name__ == '__main__':
        main()
    

    Take a look at http://docs.python.org/library/multiprocessing.html There is an example of using Array of chars.

    There is also mmap module alowing to share memory http://docs.python.org/library/mmap.html but with this you have to Sync access yourself possibly by semaphores. If you like more simple approach just use pipes.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.