Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在解决Whoosh索引错误:写入程序已关闭_Python_Indexing_File Writing_Whoosh_Readwritelock - Fatal编程技术网

Python 正在解决Whoosh索引错误:写入程序已关闭

Python 正在解决Whoosh索引错误:写入程序已关闭,python,indexing,file-writing,whoosh,readwritelock,Python,Indexing,File Writing,Whoosh,Readwritelock,与不同的是,我没有中断任何提交,但在创建新索引时会出现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.exist

与不同的是,我没有中断任何提交,但在创建新索引时会出现
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回溯(最近一次调用上次)
在()
15以ix.writer()作为编写器:
16 writer.add_文档(hashID=str(uuid.uuid4())
--->17.提交
/usr/local/lib/python3.5/site-packages/whoosh/writing.py in\uuuuuuu exit\uuuuuu(self、exc\u type、exc\u val、exc\u tb)
208 self.cancel()
209其他:
-->210 self.commit()
211
212 def组(自身):
/提交中的usr/local/lib/python3.5/site-packages/whoosh/writing.py(self、mergetype、optimize、merge)
918         """
919
-->920自我检查状态()
921#必要时合并旧段
922 finalsegments=self.\u合并\u段(合并类型、优化、合并)
/usr/local/lib/python3.5/site-packages/whoosh/writing.py处于检查状态(self)
553 def检查状态(自身):
554如果self.u关闭:
-->555 raise IndexingError(“此写入程序已关闭”)
556
557定义设置文档偏移量(自):
IndexingError:此编写器已关闭
写入程序应该在上下文范围内,因此我不确定它为什么会关闭,尽管它是新创建的。如何解决新索引上的索引错误?

writer.commit()
保存更改并关闭写入程序

然后在语句末尾,
以ix.writer()作为编写器:
尝试关闭已关闭但不存在的编写器

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

try:
    writer = ix.writer()
    writer.add_document(hashID=str(uuid.uuid4()))
    writer.commit()
finally:
    writer.commit()
作为一种解决方案,无论您是在
with
语句中省略
writer.commit()
,还是在每次要提交时使用
语句摆脱
并重新创建
writer