为什么我收到模块 'multiprocessing' 没有属性 'sharedctypes' 的错误?

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

我有一个使用多处理的 Python 程序。

在某个时候,我会:

import multiprocessing
import ctypes

...

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

...将数据 ndarray 复制到共享内存中。

令我惊讶的是,它失败了:

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'

它怎么没有 sharedctypes?在 IDE 中,如果我只是 ctrl+click 名称,我就会看到它在 sharedctypes.py 的定义。

很明显它在那里,但是当我 运行 它找不到它时?

您还需要导入子包:

import multiprocessing
import multiprocessing.sharedctypes
import ctypes