【问题标题】:Resolving Whoosh IndexingError: Writer is closed解决 Whoosh IndexingError: Writer is closed
【发布时间】:2017-05-04 02:56:16
【问题描述】:

python whoosh IndexingError when interrupted 不同,我没有中断任何提交,但IndexingError 在创建新索引时出现:

import uuid
import os

from whoosh.index import create_in
from whoosh.fields import *
from whoosh.qparser import QueryParser

schema = Schema(hashID=TEXT(stored=True))
indexdir = 'foobar'
if not os.path.exists(indexdir):
    os.mkdir(indexdir)

ix = create_in(indexdir, schema)

with ix.writer() as writer:
    writer.add_document(hashID=str(uuid.uuid4()))
    writer.commit()

错误:

---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
<ipython-input-1-85a42bebdce8> in <module>()
     15 with ix.writer() as writer:
     16     writer.add_document(hashID=str(uuid.uuid4()))
---> 17     writer.commit()

/usr/local/lib/python3.5/site-packages/whoosh/writing.py in __exit__(self, exc_type, exc_val, exc_tb)
    208             self.cancel()
    209         else:
--> 210             self.commit()
    211 
    212     def group(self):

/usr/local/lib/python3.5/site-packages/whoosh/writing.py in commit(self, mergetype, optimize, merge)
    918         """
    919 
--> 920         self._check_state()
    921         # Merge old segments if necessary
    922         finalsegments = self._merge_segments(mergetype, optimize, merge)

/usr/local/lib/python3.5/site-packages/whoosh/writing.py in _check_state(self)
    553     def _check_state(self):
    554         if self.is_closed:
--> 555             raise IndexingError("This writer is closed")
    556 
    557     def _setup_doc_offsets(self):

IndexingError: This writer is closed

作者应该在上下文范围内,所以我不确定为什么它是新创建的,但它被关闭了。 如何解决新索引上的 IndexingError?

【问题讨论】:

    标签: python indexing file-writing whoosh readwritelock


    【解决方案1】:

    writer.commit() 保存更改并关闭编写器。

    然后在语句的最后,with ix.writer() as writer: 尝试关闭已经关闭且不存在的 writer。

    因此,您的with 语句相当于:

    try:
        writer = ix.writer()
        writer.add_document(hashID=str(uuid.uuid4()))
        writer.commit()
    finally:
        writer.commit()
    

    作为一种解决方案,无论您在 with 语句中省略 writer.commit() 还是删除 with 语句并在每次要提交时重新创建 writer

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2016-05-04
      • 2019-11-04
      • 2021-07-23
      • 1970-01-01
      • 2020-08-11
      • 1970-01-01
      相关资源
      最近更新 更多