2016-04-22 64 views
7

您好我正在運行一個postgreSQL數據庫的燒瓶應用程序。使用多個工作人員時會出現LockErrors。我瞭解到,這是因爲嗖搜索鎖定數據庫在flask中使用BufferedWriter whooshalchemy

http://stackoverflow.com/questions/36632787/postgres-lockerror-how-to-investigate 

由於在這個環節我都用的BufferedWriter解釋......我谷歌身邊,但我真的不知道如何實現它?下面是在嗖

import sys 
if sys.version_info >= (3, 0): 
    enable_search = False 
else: 
    enable_search = True 
    import flask.ext.whooshalchemy as whooshalchemy 

class User(db.Model): 
    __searchable__ = ['username','email','position','institute','id'] # these fields will be indexed by whoosh 

    id = db.Column(db.Integer, primary_key=True) 
    username = db.Column(db.String(100), index=True) 
    ... 

    def __repr__(self): 
     return '<User %r>' % (self.username) 

if enable_search: 
    whooshalchemy.whoosh_index(app, User) 

幫助條款我的數據庫的設置是非常讚賞 感謝 卡爾

編輯:如果在燒瓶whosshsqlalchemy在那裏你可以建議任何替代並行訪問沒有能力?

回答

2

正如你可以在這裏閱讀:

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() 
+0

嗨米歇爾感謝您的回覆。在我的情況下,我沒有大的數據庫負載,我只是每次都會出現一個鎖錯誤,並希望避免它......像BufferedWriter這樣的標準解決方案將會是完全正確的......我無法找到一個示例如何使用它與燒瓶 - whoshalchemy – carl

+0

這是否意味着flask-whooshalchemy不提供此功能? – carl

+0

不知道...我有一個想法來創建類似於彈性搜索的東西是爲Lucene,也許與兼容接口:) –