如何让 Pool.map 采用 lambda 函数
我有以下函数:
def copy_file(source_file, target_dir):
pass
现在我想使用 multiprocessing
立即执行此函数:
p = Pool(12)
p.map(lambda x: copy_file(x,target_dir), file_list)
问题是,lambda 无法进行 pickle,因此会失败。解决这个问题最简洁的(Pythonic)方法是什么?
I have the following function:
def copy_file(source_file, target_dir):
pass
Now I would like to use multiprocessing
to execute this function at once:
p = Pool(12)
p.map(lambda x: copy_file(x,target_dir), file_list)
The problem is, lambda's can't be pickled, so this fails. What is the most neat (pythonic) way to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用函数对象:
运行
Pool.map
:Use a function object:
To run your
Pool.map
:对于 Python2.7+ 或 Python3,您可以使用 functools.partial:
For Python2.7+ or Python3, you could use functools.partial:
问题有点老了,但如果你仍在使用 Python 2,我的答案可能会有用。
技巧是使用 pathos 项目的一部分:pathos 项目:multiprocess 多处理的分支。它摆脱了原始多进程的烦人限制。
安装:
pip install multiprocess
使用方法:
Question is a bit old but if you are still use Python 2 my answer can be useful.
Trick is to use part of pathos project: multiprocess fork of multiprocessing. It get rid of annoying limitation of original multiprocess.
Installation:
pip install multiprocess
Usage:
来自这个 答案,pathos 让你直接运行 lambda
p.map(lambda x: copy_file(x,target_dir), file_list)
,保存所有解决方法/黑客From this answer, pathos let's you run your lambda
p.map(lambda x: copy_file(x,target_dir), file_list)
directly, saving all the workarounds / hacks您可以使用
starmap()
通过池化来解决这个问题。假定您有一个文件列表(例如在工作目录中),并且您有一个要将这些文件复制到的位置,那么您可以
import os
并使用os.system( )
在 python 中运行终端命令。这将使您可以轻松地移动文件。但是,在开始之前,您需要创建一个变量
res = [(file, target_dir) for file in file_list]
来容纳目标目录中的每个文件。它看起来像......
显然,对于这个用例,您可以通过将每个文件和目标目录存储在一个字符串中来简化此过程,但这会降低使用此方法的洞察力。
这个想法是,
starmap()
将获取res
的每个组件并将其放入函数copy_file(source_file, target_dir)
中并执行它们是同步的(这受到CPU核心数量的限制)。因此,第一个操作线程将类似于
copy_file('test1.pdf', '/home/mcurie/files/pdfs/')
我希望这会有所帮助。完整代码如下。
You can use
starmap()
to solve this problem with pooling.Given that you have a list of files, say in your working directory, and you have a location you would like to copy those files to, then you can
import os
and useos.system()
to run terminal commands in python. This will allow you to move the files over with ease.However, before you start you will need to create a variable
res = [(file, target_dir) for file in file_list]
that will house each file with the target directory.It will look like...
Obviously, for this use case you can simplify this process by storing each file and target directory in one string to begin with, but that would reduce the insight of using this method.
The idea is that
starmap()
is going to take each component ofres
and place it into the functioncopy_file(source_file, target_dir)
and execute them synchronously (this is limited by the core quantity of your cpu).Therefore, the first operational thread will look like
copy_file('test1.pdf', '/home/mcurie/files/pdfs/')
I hope this helps. The full code is below.