python - 不能使用从 Python 多处理返回的值
问题描述
我在一个类中的一个方法中使用了多处理技术(该类最终会被导入到主类中执行),代码段如下
result = []
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=4)
data = [[1,2],[3,4],[5,6],[7,8]]
result_m = [pool.starmap(some_function, data)]
pool.join()
pool.close()
result.append(result_m[0])
result.append(result_m[1])
result.append(result_m[2])
result.append(result_m[3])
first = result[0]
sceond = result[1]
third = result[2]
fourth = result[3]
基本上,这个想法是使用多处理在四个进程中同时调用另一个名为 some_function() 的方法,然后将每个进程的结果附加到名为“result”的列表中。但是,当我运行代码时,最后四行总是显示“列表索引超出范围”。结果似乎从未添加到之前定义的列表中。我想知道为什么会这样,有什么潜在的方法可以解决这个问题?谢谢!
解决方案
推荐阅读
- java - android:没有获得关于标记点击的信息
- ubuntu - 如何在 Ubuntu 上更新我的 Visual Studio 代码
- postgresql - 在 TimescaleDB 中支持 3D 坐标
- python - Python:如何从另一个文件访问属性?从文件名导入类不起作用
- amazon-web-services - 如何从 us-east-1 帐户访问在 us-west-2 区域创建的 S3 存储桶?
- ionic4 - 无法在 ionic 中找到 app-routing.module.ts
- node.js - at=error code=H12 desc="Request timeout" in node.js.如何处理?
- java - 如何使用 DateTimeFormatter 仅获取时间?
- tensorflow - 在 google colab 中,有没有办法检查 TPU 版本正在运行?
- swift - 如何在附加到 TextField 的 swift @Published didSet 中正确格式化字符串?