用于大型只读存储的Python Multiprocessing Manager dict的替代方案

时间:2013-10-02 07:58:47

标签: python multiprocessing

我正在使用Multiprocessing和一个由进程使用的大型(~5G)只读字典。我开始将整个dict传递给每个进程,但遇到了内存限制,所以更改为使用Multiprocessing Manager dict(在阅读此How to share a dictionary between multiple processes in python without locking之后)

自改变以来,表现已经下降。更快的共享数据存储有哪些替代方案? dict有一个40个字符的字符串键和2个小的字符串元素元组数据。

1 个答案:

答案 0 :(得分:0)

使用内存映射文件。虽然这可能听起来很疯狂(性能明智),但如果你使用一些聪明的技巧可能不会这样:

  1. 对键进行排序,以便您可以在文件中使用二进制搜索来查找记录
  2. 尝试使文件的每一行长度相同(“固定宽度记录”)
  3. 如果您不能使用固定宽度记录,请使用此伪代码:

    Read 1KB in the middle (or enough to be sure the longest line fits *twice*)
    Find the first new line character
    Find the next new line character
    Get a line as a substring between the two positions
    Check the key (first 40 bytes)
    If the key is too big, repeat with a 1KB block in the first half of the search range, else in the upper half of the search range
    

    如果表现不够好,请考虑在C中编写扩展名。