<p>我有一个脚本,在数据库中查询“blogs”,对于每个blog,启动一个线程来查询它的RSS地址,检查新的帖子并将它们记录在数据库中。最初,我用最多两个并行线程运行这个脚本(同时从至少两个博客的rss中检索信息),然后,我开始得到这个“数据库锁定错误”,现在我把它减少到一个,我仍然得到这个错误。在</p>
<p>对于数据库连接和ORM,我使用的是peewee 2.7.4,如下所示:</p>
<pre><code>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):
(...)
</code></pre>
<p>所以,开始编写脚本:</p>
^{pr2}$
<p>当然,这是脚本的简化版本,但基本上就是这样,在“posts”的第一个循环中,我在保存后():</p>
<pre><code>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
</code></pre>
<p>请记住,现在我使用MAX_THREADS=1运行,这样一次只处理一个blog。最让我困扰的是,在第一次运行时,我会用MAX_THREADS=2运行它,它会一直运行下去,很好。这个错误几天后才开始,所以我不知道是否可能在blogs select上,在主线程上,事情会被锁定(也许select是附加的,我不得不以某种方式将其分离)。有人能帮我吗?这实际上是一个很小的过程,我不想为另一个数据库引擎而改变,而且我看到了在至少2个线程中并行运行的性能优势,这也是至关重要的。在</p>
<p>非常感谢你的帮助。在</p>
<p>谢谢你,
TS公司</p>