正如你可以在這裏閱讀:
http://whoosh.readthedocs.io/en/latest/threads.html
只有一個作家能裝鎖。緩衝作家,保留你的數據一段時間,但...在某個時候你的對象被存儲,並意味着鎖定。
根據那篇文檔,異步編寫器是你正在尋找的東西,但是...如果失敗,它會嘗試存儲你的數據 - 它會創建額外的線程,然後重試。假設你投擲了1000個新物品。可能你會得到像1000線程的東西。將每個插入視爲任務可能會更好,並將其發送到單獨的線程。如果有很多進程,你可以堆疊這些任務。例如 - 插入10,然後等待。如果這10個批次在短時間內插入?將工作 - 有一段時間了...
編輯
樣品與異步讀寫器 - 使緩衝 - 只需重命名的進口和使用。
import os, os.path
from whoosh import index
from whoosh.fields import SchemaClass, TEXT, KEYWORD, ID
if not os.path.exists("data"):
os.mkdir("data")
# http://whoosh.readthedocs.io/en/latest/schema.html
class MySchema(SchemaClass):
path = ID(stored=True)
title = TEXT(stored=True)
icon = TEXT
content = TEXT(stored=True)
tags = KEYWORD
# http://whoosh.readthedocs.io/en/latest/indexing.html
ix = index.create_in("data", MySchema, indexname="myindex")
writer = ix.writer()
writer.add_document(title=u"My document", content=u"This is my document!",
path=u"/a", tags=u"first short", icon=u"/icons/star.png")
writer.add_document(title=u"Second try", content=u"This is the second example.",
path=u"/b", tags=u"second short", icon=u"/icons/sheep.png")
writer.add_document(title=u"Third time's the charm", content=u"Examples are many.",
path=u"/c", tags=u"short", icon=u"/icons/book.png")
writer.commit()
# needed to release lock
ix.close()
#http://whoosh.readthedocs.io/en/latest/api/writing.html#whoosh.writing.AsyncWriter
from whoosh.writing import AsyncWriter
ix = index.open_dir("data", indexname="myindex")
writer = AsyncWriter(ix)
writer.add_document(title=u"My document no 4", content=u"This is my document!",
path=u"/a", tags=u"four short", icon=u"/icons/star.png")
writer.add_document(title=u"5th try", content=u"This is the second example.",
path=u"/b", tags=u"5 short", icon=u"/icons/sheep.png")
writer.add_document(title=u"Number six is coming", content=u"Examples are many.",
path=u"/c", tags=u"short", icon=u"/icons/book.png")
writer.commit()
嗨米歇爾感謝您的回覆。在我的情況下,我沒有大的數據庫負載,我只是每次都會出現一個鎖錯誤,並希望避免它......像BufferedWriter這樣的標準解決方案將會是完全正確的......我無法找到一個示例如何使用它與燒瓶 - whoshalchemy – carl
這是否意味着flask-whooshalchemy不提供此功能? – carl
不知道...我有一個想法來創建類似於彈性搜索的東西是爲Lucene,也許與兼容接口:) –