admin管理员组文章数量:1128270
I need to download multiple files at once from MongoDB, and then process the result as soon as file is downloaded. But the thing is, it's not faster of sync cycle. It seems like it still waits for previous to download. Playing with max_workers didn't help.
Each file download elapsed time ~0.3, so I expect it to complete all downloads at once at least at elapsed ~0.5. But it always gives me 3 seconds for 10 files, no matter which way I download it.
I checked it multiple times and can't figure what could be wrong at this point.
I tried AsyncIOMotorClient + AsyncIOMotorGridFSBucket + asyncio, and results are worse - slower.
client = MongoClient(
host=MONGO_HOST,
port=MONGO_PORT,
)
db = client[MONGO_DB_NAME]
fs = GridFS(db)
def get_file(file_id: str) -> dict:
grid_out = fs.get(ObjectId(file_id))
contents = grid_out.read()
str_content = contents.decode()
return json.loads(str_content)
def get_values():
files_id = [...] # Guids
# Without concurency
for file_id in files_id:
get_file(file_id) # Each file elapsed ~0.3 seconds
# !!! Total elapsed time ~3 seconds
# -----------------------------------
# With concurency
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for file_id in files_id:
futures.append(executor.submit(get_file, file_id))
results = [
future.result() for future in futures
] # Each file elapsed ~0.3 seconds
# !!! Total elapsed time ~3 seconds
本文标签:
版权声明:本文标题:python 3.x - Pymongo, GridFS, ThreadPoolExecutor, download multiple files simultaneously - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736719230a1949373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论