首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Peewee python2.7数据库锁定错误

Peewee python2.7数据库锁定错误
EN

Stack Overflow用户
提问于 2016-01-25 02:39:07
回答 2查看 501关注 0票数 0

我有一个在数据库上查询“博客”的脚本,对于每个博客,启动一个线程来查询它的RSS地址,并检查新帖子并将它们记录在数据库中。最初,我使用最多两个并行线程运行这个脚本(同时从至少两个博客的rss中检索信息),然后,我开始得到这个“数据库锁定错误”,现在我将这个错误减少到一个,我仍然得到这个错误。

对于数据库连接和ORM,我使用peewee 2.7.4,如下所示:

代码语言:javascript
复制
from peewee import *
from playhouse.sqlite_ext import SqliteExtDatabase

db = SqliteExtDatabase(APP_DIR + '/ml.db')

class BaseModel(Model):
    class Meta:
       database = db

class Blog(BaseModel):
     (...)

class Post(BaseModel):
     (...)

因此,启动脚本会这样做:

代码语言:javascript
复制
def start():
    global ACTIVE_THREADS, MAX_THREADS
    blogs = Blog.select().where(Block.active=1)
    for blog in blogs:
        while ACTIVE_THREADS == MAX_THREADS:
            print 'Max number of threads %d reached. zzzz' % MAX_THREADS
            time.sleep(1)
        blog.processing=1
        blog.save()

        ACTIVE_THREADS += 1

        th = threading.Thread(target=process_blog, args=(blog,))
        th.daemon = True
        th.start()

def process_blog(blog):
    globals ACTIVE_THREADS
    get_new_posts_url_for_blog(blog) # here Post records are created with downloaded=0
    posts = Post.select().where(Post.downloaded = 0)
    for post in posts:
        content = get_content_for_post(post.url)
        post.content = content
        post.downloaded = 1
        post.save() #This is where the database locked error is thrown :(
     ACTIVE_THREADS -= 1

当然,这是脚本的简化版本,但基本上就是这样,在"posts“的第一个循环中,我在post.save()上得到了以下错误:

代码语言:javascript
复制
File "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 4573, in save
rows = self.update(**field_dict).where(self._pk_expr()).execute()
File "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3013, in execute
return self.database.rows_affected(self._execute())
File "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 2555, in _execute
return self.database.execute_sql(sql, params, self.require_commit)
File "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3366, in execute_sql
self.commit()
File "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3212, in __exit__
reraise(new_type, new_type(*exc_args), traceback)
File "/home/thilux/virtual_envs/ptmla/local/lib/python2.7/site-packages/peewee.py", line 3359, in execute_sql
cursor.execute(sql, params or ())
OperationalError: database is locked

请记住,现在,我使用的是MAX_THREADS=1,所以一次只有一个博客在处理。最让我困扰的是,在第一次运行时,我会用MAX_THREADS=2运行它,它会一直运行下去,一切都很好。这个错误是几天后才开始的,所以我不知道是否在博客select上,在主线程上,东西被锁定了(可能select被附加了,我不得不以某种方式分离它们)。有没有人能帮我一下?这真的是一个很小的过程,我不想改变另一个数据库引擎,我看到了性能的好处,这也是至关重要的,因为至少在2个线程中并行运行。

非常感谢您的帮助。

谢谢你,TS

EN

回答 2

Stack Overflow用户

发布于 2016-01-26 02:39:16

尝试使用atomic()上下文管理器包装您的代码。

票数 0
EN

Stack Overflow用户

发布于 2016-01-25 09:31:11

我不知道peewee是什么,在没有看到实际执行的SQL查询的情况下,我不能确切地说出问题出在哪里。您可以做两件事: 1.检查是否有一种方法可以打印出正在执行的实际SQL语句。2.查看this link了解可能的原因。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34979823

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档