使用BufferedWriter in flask whooshalchemy

时间:2016-04-22 19:47:20

标签: python flask-sqlalchemy whoosh flask-whooshee

您好我正在运行带有postgreSQL数据库的烧瓶应用程序。使用多个worker时,我得到LockErrors。我了解到这是因为嗖嗖搜索会锁定数据库

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

正如在这个链接中所解释的,我必须使用BufferedWriter ...我谷歌周围,但我真的无法弄清楚如何实现它?这是我在whoosh

方面的数据库设置
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你有什么选择吗?

1 个答案:

答案 0 :(得分: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()