2

I have the hardest time to share a string between processes. I looked at the following Q1, Q2, Q3 but still fail in an actual example. If I understood the docs correctly, looking should be done automatically therefore there should be no race condition with setting/reading. Here's what I've got:

#!/usr/bin/env python3

import multiprocessing
import ctypes
import time

SLEEP = 0.1
CYCLES = 20

def child_process_fun(share):
    for i in range(CYCLES):
        time.sleep(SLEEP)
        share.value = str(time.time())


if __name__ == '__main__':
    share = multiprocessing.Value(ctypes.c_wchar_p, '')
    process = multiprocessing.Process(target=child_process_fun, args=(share,))
    process.start()

    for i in range(CYCLES):
        time.sleep(SLEEP)
        print(share.value)

which produces:

Traceback (most recent call last):
    File "test2.py", line 23, in <module>
       print(share.value)
    File "<string>", line 5, in getvalue
ValueError: character U+e479b7b0 is not in range [U+0000; U+10ffff]

Edit: ´id(share.value)´ is different for each process. However if I try a double as shared variable instead they are the same and it works like a charm. Could this be a python bug?

magu_
  • 4,766
  • 3
  • 45
  • 79
  • Did you take a look at the [docs](https://docs.python.org/2/library/multiprocessing.html#exchanging-objects-between-processes)? – pstatix Dec 12 '17 at 01:05
  • @pstatix I did, but I was under the impression that having shared memory does not require going over pipes or similar in order to share a state. And that multiprocessing.Value (https://docs.python.org/3.6/library/multiprocessing.html?highlight=multiprocessing%20value#multiprocessing.Value) should do this job. – magu_ Dec 12 '17 at 02:12
  • Your processes are separate, how do you expect them to know the address of a process specific variable within the other? – pstatix Dec 12 '17 at 04:27
  • If you want to stick with `Value` look [here](https://eli.thegreenplace.net/2012/01/04/shared-counter-with-pythons-multiprocessing) – pstatix Dec 12 '17 at 04:34
  • Did you ever solve this? – gshpychka Nov 27 '21 at 09:43

0 Answers0