gpt4 book ai didi

python - 优雅地编码尝试 : except chain

转载 作者:行者123 更新时间:2023-12-02 00:39:25 24 4
gpt4 key购买 nike

我有一个concurrent.futures,通常与 chunksize=1 一起使用。但是,有时我会遇到需要更大块大小的大型数据集。目前我已经用以下代码解决了这个问题:

for i in datasets:
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
results=tuple(executor.map(do_something, parameters, chunksize=1)
except concurrent.futures.process.BrokenProcessPool:
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
results=tuple(executor.map(do_something, parameters, chunksize=2)
except concurrent.futures.process.BrokenProcessPool:
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
results=tuple(executor.map(do_something, parameters, chunksize=4)
etc. etc. etc....
except concurrent.futures.process.BrokenProcessPool:
print('code failed')

这工作正常,但显然非常不优雅且丑陋。不管怎样,我可以更简单地做到这一点吗?

最佳答案

您可以使用 for 循环遍历 block 大小,如果成功则中断循环,或者输入 else block 输出错误信息:

for i in datasets:
for chunksize in 1, 2, 4:
try:
with concurrent.futures.ProcessPoolExecutor() as executor:
results=tuple(executor.map(do_something, parameters, chunksize=chunksize)
break
except concurrent.futures.process.BrokenProcessPool:
pass
else:
print('code failed')

关于python - 优雅地编码尝试 : except chain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59669791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com