这些评论越来越难以理解,所以我把我的重要评论粘贴在这里:
在Linux-y系统上,新进程是由fork()创建的,因此在创建它们时获取整个父进程地址空间的副本。它是“写时拷贝”,所以它更像是“虚拟”拷贝而不是“真实”拷贝,但仍然。。。;—)首先,尝试在创建巨大的数据结构之前创建Pool。然后子进程将继承更小的地址空间。在
然后回答一些问题:so in python 2.7, there is no way to spawn a new process?
在Linux-y系统上,没有。在那些系统上使用“spawn”的功能是在python3.4中首次添加的。在Windows系统上,“spawn”一直是唯一的选择(在Windows上不是fork())。在The big dictionary is passed in to a function as an argument and I
could only create the pool inside this function. How would I be able
to create the pool before the big dictionary
简单到这样:将这两行作为程序的前两行:import multiprocessing
pool = multiprocessing.Pool()
您可以随时创建池(只要它在实际使用之前存在于某个时间),工作进程将在调用Pool构造函数时继承整个地址空间。在
另一个建议
如果您在dict创建后没有对其进行突变,请尝试使用以下方法:
^{pr2}$
这样你就不必去实现一个庞大的钥匙组。或者,更好的方法是(如果可能的话),跳过所有这些,直接在这些项上迭代:for result in pool.imap_unordered(func_process_application, very_large_dict.iteritems()):