classification
Title: multiprocessing sharedctypes Array don't accept strings
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: bred, docs@python, python-dev, sbt
Priority: normal Keywords:

Created on 2012-09-10 09:59 by bred, last changed 2012-09-10 12:09 by sbt. This issue is now closed.

Messages (3)
msg170171 - (view) Author: Simon (bred) Date: 2012-09-10 09:59
I've simply tested the example reported in the py3k documentation, and it don't works.

See the site:
http://docs.python.org/py3k/library/multiprocessing.html?highlight=multiprocessing#module-multiprocessing.sharedctypes

The program exit with this error:

> python sha.py
Traceback (most recent call last):
  File "sha.py", line 21, in <module>
    s = Array('c', 'hello world', lock=lock)
  File "/usr/lib/python3.2/multiprocessing/sharedctypes.py", line 112, in Array
    obj = RawArray(typecode_or_type, size_or_initializer)
  File "/usr/lib/python3.2/multiprocessing/sharedctypes.py", line 89, in RawArray
    result.__init__(*size_or_initializer)
TypeError: one character string expected


Observe that the following code works correctly with python2!

I'm using python 3.2.3 and gcc 4.7.1 under ArchLinx 
 
Te code is:

http://docs.python.org/py3k/library/multiprocessing.html?highlight=multiprocessing#module-multiprocessing.sharedctypes

####################################################################

from multiprocessing import Process, Lock
from multiprocessing.sharedctypes import Value, Array
from ctypes import Structure, c_double

class Point(Structure):
    _fields_ = [('x', c_double), ('y', c_double)]

def modify(n, x, s, A):
    n.value **= 2
    x.value **= 2
    s.value = s.value.upper()
    for a in A:
        a.x **= 2
        a.y **= 2

if __name__ == '__main__':
    lock = Lock()

    n = Value('i', 7)
    x = Value(c_double, 1.0/3.0, lock=False)
    s = Array('c', 'hello world', lock=lock)
    A = Array(Point, [(1.875,-6.25), (-5.75,2.0), (2.375,9.5)], lock=lock)

    p = Process(target=modify, args=(n, x, s, A))
    p.start()
    p.join()

    print((n.value))
    print((x.value))
    print((s.value))
    print([(a.x, a.y) for a in A])
msg170172 - (view) Author: Richard Oudkerk (sbt) * (Python committer) Date: 2012-09-10 10:18
The documentation needs updating for Python 3 so that a byte string is used.  So the line becomes

    s = Array('c', b'hello world', lock=lock)
msg170182 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-09-10 12:08
New changeset ddb406904be1 by Richard Oudkerk in branch '3.2':
Issue #15901: Change example to use byte string instead of string
http://hg.python.org/cpython/rev/ddb406904be1
History
Date User Action Args
2012-09-10 12:09:27sbtsetstatus: open -> closed
resolution: fixed
stage: resolved
2012-09-10 12:08:02python-devsetnosy: + python-dev
messages: + msg170182
2012-09-10 10:31:57bredsetassignee: docs@python

type: crash -> behavior
components: + Documentation, - ctypes
nosy: + docs@python
2012-09-10 10:18:20sbtsetnosy: + sbt
messages: + msg170172
2012-09-10 09:59:39bredcreate