问题描述
我正在尝试在运行Debian和Python 2.7.1的Web服务器上使用ZODB 3.10.2.似乎每次我尝试从2个不同的进程访问相同的数据库时,都会得到一个神秘的异常.我尝试从交互式Python会话访问数据库,但一切似乎都正常进行:
I am trying to use ZODB 3.10.2 on my web server which is running Debian and Python 2.7.1. It seems like every time I try to access the same database from 2 different processes, I get a mysterious exception. I tried accessing a database from an interactive Python session and everything seemed to work fine:
>>> import ZODB
>>> from ZODB.FileStorage import FileStorage
>>> storage = FileStorage("test.db")
>>>
但是后来我尝试了同时运行的另一个会话中的相同命令系列,但似乎没有用:
But then I tried the same series of commands from another session running at the same time and it didn't seem to work:
>>> import ZODB
>>> from ZODB.FileStorage import FileStorage
>>> storage = FileStorage("test.db")
No handlers could be found for logger "zc.lockfile"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/ZODB3-3.10.2-py2.7-linux-x86_64.egg/ZODB/FileStorage/FileStorage.py", line 125, in __init__
self._lock_file = LockFile(file_name + '.lock')
File "/usr/local/lib/python2.7/site-packages/zc.lockfile-1.0.0-py2.7.egg/zc/lockfile/__init__.py", line 76, in __init__
_lock_file(fp)
File "/usr/local/lib/python2.7/site-packages/zc.lockfile-1.0.0-py2.7.egg/zc/lockfile/__init__.py", line 59, in _lock_file
raise LockError("Couldn't lock %r" % file.name)
zc.lockfile.LockError: Couldn't lock 'test.db.lock'
>>>
为什么会这样?该怎么办?
Why is this happening? What can be done about it?
推荐答案
ZODB不支持多进程访问.这就是为什么您会得到锁定错误的原因. ZODB文件存储已被一个进程锁定,以防止其他进程对其进行更改.
The ZODB does not support multi-process access. This is why you get the lock error; the ZODB file storage has been locked by one process to prevent other processes altering it.
有几种解决方法.最简单的选择是使用 ZEO . ZEO扩展了ZODB机制,以提供对网络上对象的访问,您可以轻松地将ZODB配置为访问ZEO服务器而不是本地FileStorage文件:
There are several ways around this. The easiest option is to use ZEO. ZEO extends the ZODB machinery to provide access to objects over a network, and you can easily configure your ZODB to access a ZEO server instead of a local FileStorage file:
<zodb>
<zeoclient>
server localhost:9100
</zeoclient>
</zodb>
另一种选择是使用 RelStorage ,它将ZODB数据存储在关系型数据库. RelStorage支持PostgreSQL,Oracle和MySQL后端. RelStorage负责来自不同ZODB客户端的并发访问.这是一个示例配置:
Another option is to use RelStorage, which stores the ZODB data in a relational database. RelStorage supports PostgreSQL, Oracle and MySQL backends. RelStorage takes care of concurrent access from different ZODB clients. Here is an example configuration:
<zodb>
<relstorage>
<postgresql>
# The dsn is optional, as are each of the parameters in the dsn.
dsn dbname='zodb' user='username' host='localhost' password='pass'
</postgresql>
</relstorage>
</zodb>
RelStorage需要更多的前期设置工作,但在许多情况下都可以胜过ZEO.
RelStorage requires more up-front setup work but can outperform ZEO in many scenarios.
这篇关于ZODB中的zc.lockfile.LockError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!