python 共享内存 list_Python多处理中共享内存(list)的同步写入

在您的示例中,同步缺少一点。manager.list只是一个普通的list,您的工作进程可以通过代理对象修改它。您的后续进程碰巧同时检查len(my_array)。在

没有同步,这告诉他们应该等到另一个进程完成了它的操作,包括执行这个长度检查,并根据这个检查的结果执行一个操作。更新操作不是原子操作,需要使用经理.lock你的行动。在

代码中还有另一个问题,即重新绑定my_array指向普通列表['y'],而不是更新/修改共享的manager.list。您不是用设置了my_array = ['y']的进程修改{},manager.list从第一次修改到第一个工作进程直到程序结束,它的值['a', 'b', 'c', 'x']。在from multiprocessing import Process, Manager

def f(my_array, lock):

with lock:

if len(my_array) < 4:

my_array.append('x')

else:

my_array[:] = [] # clear list inplace by assigning

# empty list to slice of manager.list

my_array.append('y')

print(my_array)

if __name__ == '__main__':

N_WORKERS = 5

with Manager() as manager:

my_array = manager.list(['a', 'b', 'c'])

lock = manager.Lock()

pool = [

Process(target=f, args=(my_array, lock)) for _ in range(N_WORKERS)

]

for p in pool:

p.start()

for p in pool:

p.join()

# Leaving the context-manager block will shut down the manager-process.

# We need to convert the manager-list to a normal list in the parent

# to keep its values available for further processing in the parent.

result = list(my_array)

print(f'result: {result}')

输出示例:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: python 与 c 可以通过 ctypes 模块来实现内存共享。 下面是一个简单的例子: 首先,我们在 c 定义一个函数,它接受一个整数指针和一个整数作为参数,并将整数加 1: ``` void add_one(int *x, int n) { for (int i = 0; i < n; i++) { x[i] += 1; } } ``` 然后我们可以使用 python 的 ctypes 模块来调用这个函数: ``` import ctypes # 加载动态链接库 lib = ctypes.cdll.LoadLibrary('./libfoo.so') # 定义参数类型 lib.add_one.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.c_int] # 创建一个整数数组,并将其转换为 c_int 类型的指针 arr = [1, 2, 3, 4] arr_p = (ctypes.c_int * len(arr))(*arr) arr_p = ctypes.cast(arr_p, ctypes.POINTER(ctypes.c_int)) # 调用 add_one 函数 lib.add_one(arr_p, len(arr)) # 打印修改后的数组 print(list(arr_p)) # 输出: [2, 3, 4, 5] ``` 这样,我们就在 python 调用了 c 函数,并通过 ctypes 模块在 python 使用 c 指针来实现内存共享。 ### 回答2: Python和C都是常见的编程语言,虽然在某些方面有所不同,但它们可以通过一些方法实现内存共享。 首先,Python可以通过ctypes库调用C语言编写的代码,这样就可以在Python共享内存。例如,我们可以在C创建一个包含一些数据的数组,并使用ctypes库将其传递给PythonPython可以直接访问该数组,实现内存共享。 另外,Python还可以使用共享内存模块multiprocessing.shared_memory实现与C的内存共享。该模块允许Python进程创建一个共享内存段,并将其传递给C进程,以便它们可以通过该共享内存进行通信。这种方法可以在Python和C之间高效地共享大量数据。 此外,Python还支持使用共享内存来实现多进程之间的内存共享。通过使用multiprocessing模块,Python进程可以创建一个共享内存对象,并将其传递给其他Python或C进程。这些进程可以通过该共享内存进行数据共享。 综上所述,Python和C可以通过使用ctypes库、共享内存模块multiprocessing.shared_memory或multiprocessing模块来实现内存共享。这些方法可以帮助Python与C之间有效地传递数据,以实现内存共享。 ### 回答3: Python与C可以通过共享内存进行通信和数据传输。在C语言,我们可以使用共享内存区域,然后导出这个内存区域的标识符,使其可以被其他进程访问。然后,在Python,我们可以通过ctypes模块来访问同样的内存区域。 下面是一个示例,展示了如何在Python和C之间实现内存共享: C代码: ```c #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #define SHM_SIZE 1024 int main() { key_t key; int shmid; char *data; // 创建共享内存 key = ftok(".", 'R'); shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666); // 连接到共享内存 data = shmat(shmid, NULL, 0); // 写入数据到共享内存 sprintf(data, "Hello from C"); // 断开与共享内存的连接 shmdt(data); return 0; } ``` Python代码: ```python import ctypes # 定义共享内存大小 SHM_SIZE = 1024 # 使用ctypes库加载C代码的共享库 shared_library = ctypes.CDLL("./shared_library.so") # 创建共享内存 shared_library.create_shared_memory.argtypes = ctypes.c_int shared_library.create_shared_memory.restype = ctypes.c_void_p shm = shared_library.create_shared_memory(SHM_SIZE) # 连接到共享内存 shared_library.attach_to_shared_memory.argtypes = ctypes.c_void_p shared_library.attach_to_shared_memory.restype = ctypes.c_void_p data = ctypes.cast(shared_library.attach_to_shared_memory(shm), ctypes.POINTER(ctypes.c_char)) # 读取共享内存的数据 print("Data from C:", data[:].decode()) # 断开与共享内存的连接 shared_library.detach_from_shared_memory.argtypes = ctypes.c_void_p shared_library.detach_from_shared_memory(data) ``` 上述代码在C端创建了一个大小为1024字节的共享内存区域,并向其写入了数据。然后在Python端,我们使用ctypes库加载了C代码的共享库,并对共享内存进行了连接和读取操作。 请注意,上述代码只是一个简单的示例,实际应用需要注意共享内存同步和互斥,以及异常处理等问题,以确保共享内存区域的正确访问和数据安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值