python - 多处理搜索,无需在内存中重复索引

标签 python search whoosh multiprocess

我必须在一大堆科学期刊文章中搜索我在单独文件中的一些特定文章。我的方法是使用 Whoosh 从大表中构建搜索索引,然后在索引中搜索分离文件的每一篇文章。这很有效,但需要很长时间(约 2 周)。所以我想通过实现多处理来加快速度,这就是我正在努力的地方。

没有多重处理的“简单”搜索的基本部分如下所示:

articles = open('AuthorArticles.txt', 'r', encoding='utf-8').read().splitlines()

fs = FileStorage(dir_index, supports_mmap=False)
ix = fs.open_index()
with ix.searcher() as srch:
   for article in articles:
      # do stuff with article
      q = QueryParser('full_text', ix.schema).parse(article)
      res = srch.search(q, limit=None)
      if not res.is_empty():
         with open(output_file, 'a', encoding='utf-8') as target:
            for r in res:
               target.write(r['full_text'])

现在,我特别想要实现的是将索引加载到内存中,然后多个进程访问它并搜索文章。到目前为止我的尝试如下:

 articles = open('AuthorArticles.txt', 'r', encoding='utf-8').read().splitlines()

def search_index(article):
   fs = FileStorage(dir_index, supports_mmap=True)
   ix = fs.open_index()
   with ix.searcher() as srch:
      result = []
      for a in article
         # do stuff with article
         q = QueryParser('full_text', ix.schema).parse(q)
         res = srch.search(q, limit=None)
         if not res.is_empty():
            for r in res:
               result.extend[r['full_text']]
   return result

if __name__ == '__main__':
   with Pool(4) as p:
      results = p.map(search_index, articles, chunksize=100)
      print(results)

但是,据我了解,这样每个进程都会将索引加载到内存中(由于索引相当大,所以这不起作用)。

有什么方法可以以相对简单的方式实现我所需要的吗?基本上我想做的就是使用手头的全部计算能力来搜索索引。

最佳答案

您可以使用multiprocessing shared ctypes在进程之间共享内存。如果您只需要读取访问权限,可以将 lock=False 传递给 ValueArray

也许这个答案可以帮助您走得更远:
How to combine Pool.map with Array (shared memory) in Python multiprocessing?

关于python - 多处理搜索,无需在内存中重复索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691784/

相关文章:

python - Whoosh 索引查看器

python - Paramiko 在使用 ssh-keygen 命令时挂起

python - 在 Sphinx 中创建 LaTeX 数学宏

python - 如何通过图像处理保存图像 [Raspberry Pi]

java - 字符串上的正则表达式,用于匹配和匹配,无论顺序如何

Python Whoosh - 合并结果

python - 我在运行 train.py 时遇到问题,我很困惑

search - 如何在vim中搜索某个值,然后使用该值在同一行中进行搜索和替换?

search - 为什么indexed_search仅在TYPO3 8/9中的子页面上不起作用?

python - Django Haystack 索引不止一种模型