class multiprocessing.managers.SyncManager
这个类型的对象使用 multiprocessing.Manager() 创建。
| 方法 | 描述 |
|---|---|
| Barrier(parties[, action[, timeout]]) | |
| BoundedSemaphore([value]) | |
| Condition([lock]) | |
| Event() | |
| Lock() | |
| Namespace() | |
| Queue([maxsize]) | |
| RLock() | |
| Semaphore([value]) | |
| Array(typecode, sequence) | |
| Value(typecode, value) | |
| dict() | |
| dict(mapping) | |
| dict(sequence) | |
| list() | |
| list(sequence) |
1.Manager中的共享类型都是阻塞,进程安全的,在处理完一个进程的请求后才会处理另一个进程的请求
2.符合类型中,对应的地址内容不会被修改
dict
import multiprocessing
mg = multiprocessing.Manager()
dicMP = mg.dict() # 多进程,共享字典
# temp_dict['test'] = {} # 字典key对应的地址内容不会被修改
def test(idx, test_dict):
test_dict[idx] = idx
# test_dict['test'][idx] = idx # 字典key对应的地址内容不会被修改
pool = multiprocessing.Pool(4)
for i in range(10):
pool.apply_async(test, args=(i, dicMP))
pool.close()
pool.join()
print(dicMP)
参考:
https://docs.python.org/zh-cn/3/library/multiprocessing.html#multiprocessing.managers.SyncManager
https://segmentfault.com/a/1190000018619281
本文深入探讨了Python中multiprocessing模块的SyncManager类,详细介绍了如何使用Manager()创建共享对象,如字典、列表等,并确保它们在多个进程间的同步和安全性。通过实例演示了在多进程环境下如何正确地使用这些共享类型。

1183

被折叠的 条评论
为什么被折叠?



