0
的多重条款在Python 3,你现在可以打开一个文件安全地使用with
条款是这样的:可以用Python
with open("stuff.txt") as f:
data = f.read()
使用这种方法,我并不需要担心关闭连接
我想知道如果我可以做同样的多处理。例如,我当前的代码看起来像:
pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
pool.starmap(function,list)
pool.close()
pool.join()
有什么办法,我可以用一用子句来简化这个?
要么使用'try catch'块,要么编写一个[上下文管理器](https://jeffknupp.com/blog/2016/03/07/python-with-context-managers/)来包装它。 – spectras
在这里:[上传管理器(“与”语句)“](https://stackoverflow.com/documentation/python/928/context-managers-with-statement)在stackoverflow文档。 – spectras