CGIs and file exclusion
Michele Simionato
michele.simionato at gmail.com
Fri Nov 5 05:52:35 EST 2004
"Diez B. Roggisch" <deetsNOSPAM at web.de> wrote in message news:<cmecq2$ksm$00$1 at news.t-online.com>...
> I'd suggest using ZODB instead of directly pickling - then every cgi can
> open the database with its own connection. ZODB will manage concurrency
> issues.
Ok, but then I guess you need an external long-living process keeping the
DB open for you. If the cgi script opens and close the database by itself,
the only thing the ZODB buys for you is raising
IOError: [Errno 11] Resource temporarily unavailable
(not unexpectedly). Here is what I tried:
$ cat concurrency.py
import ZODB
from ZODB.FileStorage import FileStorage
def openzodb(dbname):
db = ZODB.DB(FileStorage(dbname + ".fs"))
conn = db.open()
return db, conn, conn.root()
if __name__ == "__main__":
print "Opening the db ..."
db, conn, root = openzodb("x")
print "Storing something ..."
root["somekey"] = "somedata"
get_transaction().commit()
print "Closing the db ..."
conn.close(); db.close()
$ echo Makefile
default:
python concurrency.py&
python concurrency.py
$ make
python concurrency.py&
python concurrency.py
Opening the db ...
Opening the db ...
Traceback (most recent call last):
File "concurrency.py", line 12, in ?
db, conn, root = openzodb("x")
File "concurrency.py", line 6, in openzodb
db = ZODB.DB(FileStorage(dbname + ".fs"))
File "/opt/zope/lib/python/ZODB/FileStorage.py", line 232, in __init__
Storing something ...
self._lock_file = LockFile(file_name + '.lock')
File "/opt/zope/lib/python/ZODB/lock_file.py", line 62, in __init__
lock_file(self._fp)
File "/opt/zope/lib/python/ZODB/lock_file.py", line 42, in lock_file
fcntl.flock(file.fileno(), _flags)
IOError: [Errno 11] Resource temporarily unavailable
Closing the db ...
BTW, it is clear from the traceback than internally ZODB uses fcntl
and probably a custom solution based on it would be simpler than
installing the ZODB (unless the OP has some reason to want it).
But I guess you had in mind something different, care to explain?
Michele Simionato
More information about the Python-list
mailing list