classification
Title: multiprocessing Array create for ctypes.c_char, TypeError unless 1 char string arg used
Type: behavior Stage: resolved
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: davin, iritkatriel, jgschaefer
Priority: normal Keywords:

Created on 2017-05-16 14:58 by jgschaefer, last changed 2021-06-18 21:34 by iritkatriel. This issue is now closed.

Messages (4)
msg293765 - (view) Author: John Schaefer (jgschaefer) Date: 2017-05-16 14:58
When creating a multiprocessing Array equivalent for unsigned chars, eg from a numpy.uint8 array, the first argument: typecode_or_type must be specified as a one character string, if a typecode is used the method raises a TypeError.

>>> import numpy as np, multiprocessing as mp, ctypes
>>> arr = np.array(range(10), dtype=np.uint8)
>>> arr
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=uint8)
>>> mp.Array("B", arr)
<SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_ubyte_Array_10 object
>>> mp.Array(ctypes.c_char, arr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/multiprocessing/__init__.py", line 260, in Array
    return Array(typecode_or_type, size_or_initializer, **kwds)
  File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 120, in Array
    obj = RawArray(typecode_or_type, size_or_initializer)
  File "/usr/lib/python2.7/multiprocessing/sharedctypes.py", line 94, in RawArray
    result.__init__(*size_or_initializer)
TypeError: one character string expected

This contrasts with behavior exhibited with other types:
>>> farr = np.array(range(10), dtype=np.float)
>>> mp.Array(ctypes.c_double, farr)
<SynchronizedArray wrapper...

thanks, John
msg293766 - (view) Author: Davin Potts (davin) * (Python committer) Date: 2017-05-16 15:11
Maybe I missed your point but why would you not want to do this instead?

>>> mp.Array(ctypes.c_int8, arr)
<SynchronizedArray wrapper for <multiprocessing.sharedctypes.c_byte_Array_10 object at ...>>
msg293767 - (view) Author: Davin Potts (davin) * (Python committer) Date: 2017-05-16 15:12
Perhaps I should've used ctypes.c_uint8 in that example/question instead.
msg396099 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-18 21:34
I'm assuming Davin's alternative solved your problem. If not, please check that you're still seeing it version 3.9+ and create a new issue.
History
Date User Action Args
2021-06-18 21:34:39iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg396099

resolution: not a bug
stage: resolved
2017-05-16 15:12:15davinsetmessages: + msg293767
2017-05-16 15:11:15davinsetnosy: + davin
messages: + msg293766
2017-05-16 14:58:51jgschaefercreate