Search code examples
pythonmultiprocessingpython-multiprocessingshared-memory

Why do I get an error that reads module 'multiprocessing' has no attribute 'sharedctypes'?


I have a Python program that uses multiprocessing.

At a certain point, I do:

import multiprocessing
import ctypes

...

# data is an ndarray
sm_data = multiprocessing.sharedctypes.RawArray(ctypes.c_double, data.flatten())

...to copy the data ndarray into shared memory.

To my surprise, it fails with:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/home/stark/anaconda3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/home/stark/anaconda3/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/stark/Work/mmr/GpuVm_CUDA.py", line 266, in run_simulation
    sm_data = multiprocessing.sharedctypes.RawArray(ctypes.c_double, data.flatten())
AttributeError: module 'multiprocessing' has no attribute 'sharedctypes'

How on Earth don't it has sharedctypes? In the IDE, if I just ctrl+click the name I am taken to its definition at sharedctypes.py.

So clearly its there, but when I run it can't find it?


Solution

  • You need to import the sub-packages as well:

    import multiprocessing
    import multiprocessing.sharedctypes
    import ctypes