python - 在 Python 中使用多处理进行递归处理
问题描述
我正在寻找递归(数据处理)代码的最简单实现,同时利用多个进程。目标功能类似于处理文件夹(-子文件夹)结构中的文件,因此有很大的并行空间(同一文件夹中的许多文件),但递归使事情变得具有挑战性。
这是我到目前为止生成的一段代码:
#coding UTF-8
import multiprocessing as mp
iThreads = mp.cpu_count()
# Dummy data structure; balanced binary tree, numbers are indexes of child nodes
testList = [[1, 2],
[3, 4], [5, 6],
[7, 8], [9, 10], [11, 12], [13, 14],
[15, 16], [17, 18], [19, 20], [21, 22], [23, 24], [25, 26], [27, 28], [29, 30],
[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [],
]
m = mp.Manager()
taskList = m.list()
def procList(inListItem):
taskList.append(inListItem)
processList = [mp.Process(target=procList, args=(li, )) for li in testList[inListItem]]
map(mp.Process.start, processList)
map(mp.Process.join, processList)
# For readability: less dense:
# processList = []
# for li in testList[inListItem]:
# p = mp.Process(target=procList, args=(li,))
# processList.append(p)
# p.start()
#
# for p in processList:
# p.join()
# For Python 3, enforcing looping through iterable map objects:
# list(map(mp.Process.start, processList))
# list(map(mp.Process.join, processList))
if __name__ == "__main__":
procList(0)
print taskList
- 理论上演练是深度优先的,但显然多处理搞砸了,这不是问题
- 最初我正在尝试使用 multiprocessing.Pool,但工作人员的数量是有限的,如果我没有达到这个数字的全部深度,我猜整个程序会冻结(每个工作人员都在等待另一个工作人员被释放以达到递归的全部深度)
- 此代码适用于 Python 2。 map() 函数不会遍历 map 对象,因此必须对其进行 enfroce
我要问的是这段代码是否正确(我是一个多处理菜鸟)
解决方案
推荐阅读
- firebase - 我可以使用哪个 Firebase api Rest 从集合中获取所有文档列表
- reactjs - 如何通过 Context native 传递数组
- vba - 如何使具有早期绑定的vba无处不在?
- java - Android Studio:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法
- wordpress - 用于父子帖子的自定义帖子类型的 WordPress 重写规则 API 触发 404.php
- google-apps-script - Google Apps 脚本更新撰写收件人
- javascript - 在 LatLngs 池中找到最大 LatLng
- javascript - 如何通过在另一个输入时间上添加分钟来设置输入时间的值
- javascript - DOMException: 权限被拒绝 - navigator.mediaDevices.getUserMedia();
- asp.net-web-api - ASP.NET WEB API:如果 CORS 被禁用,为什么我可以从 localhost 发布到 web api?