python - Python多处理:不尊重chunksize参数,总是发送1个项目
问题描述
在这里阅读@Darkonaut 的答案后,我仍然无法将我的可迭代批次发送到函数进行多处理。
我的设置:
$ python --version
Python 3.6.7 :: Anaconda, Inc.
OS X 10.15.7
Also tested on RHEL 8, same result.
这是一个带有以下输出的最小示例
import multiprocessing
from itertools import repeat
iterable = list(range(1000))
def dummy_func(arg1, arg2):
if hasattr( arg1, "__len__"):
print(f"Batch size: {len(arg1)}")
else:
print("Single item sent to func.")
print(f"Arg1: {arg1}, Arg2: {arg2}")
with multiprocessing.Pool(processes=8) as pool:
pool.starmap(
dummy_func,
zip(
iterable,
repeat("Static second variable")),
chunksize = 10)
pool.close()
pool.join()
无论 chunksize 是 None 还是大于 1 的任何数字,它都会发送单个项目。输出:
Single item sent to func.
Arg1: 0, Arg2: Static second variable
Single item sent to func.
Arg1: 1, Arg2: Static second variable
Single item sent to func.
Arg1: 2, Arg2: Static second variable
...
Arg1: 989, Arg2: Static second variable
我的期望是 arg1 将是长度为块大小的项目列表,这不正确吗?
解决方案
推荐阅读
- android - 在 QT 中实现支持库
- tensorflow - 在张量流中将一个单词剥离为其组成字符
- python - 在同一类下使用更改 xpath 刮取元素
- java - HBase shell 崩溃并给出 CMSIncrementalMode 错误
- angular - 如何在 ng-bootstrap Carousel 中获取索引图像
- unity3d - 奇怪的 Instatiate 健康栏
- python - 如何格式化从多对多关系访问的元素?
- visual-studio-2017 - Azure DevOps 中已删除的项目以其 ID 保留在 Visual Studio 中
- linux - 从win10到linux的SSH连接脚本
- android - 使用 Android Studio 创建新的 Flutter 项目时出现问题