librelist archives

« back to archive

LockError using flask-zodb

LockError using flask-zodb

From:
Alex
Date:
2012-05-19 @ 22:10
Hi all!

I have a problem using flask-zodb in a simple flask app deployed with mod_wsgi.
Every time I try to use the DB for example setting defaults:

from flaskext.zodb import zodb, List

db = ZODB(app)
app.config.from_pyfile('settings.py') # here I have defined
ZODB_STORAGE = /home/username/webapps/myapp/htdocs/Data.fs

@app.before_request
def set_db_defaults():
    if 'entries' not in db:
        db['entries'] = List()

or in a view like:

@app.route('/add', methods=['POST'])
def add_entry():
    db['entries'].append(request.form)
    flash('New entry was successfully posted')
    return redirect(url_for('show_entries'))

I got the following error:

[Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]
self._lock_file = LockFile(file_name + '.lock')
[Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File

"/home/userame/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
line 76, in __init__
[Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     _lock_file(fp)
[Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File

"/home/username/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
line 59, in _lock_file
[Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     raise
LockError("Couldn't lock %r" % file.name)
[Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] LockError:
Couldn't lock '/home/username/webapps/myapp/htdocs/Data.fs.lock'


I can't understand why this happens and how to avoid this kind of
error. Any ideas?

Alex

Re: [flask] LockError using flask-zodb

From:
Karsten Hoffmann
Date:
2012-05-21 @ 14:31
Hi,

Maybe this fragment is useful for your problem. I'm an absolut Flask
beginner so excuse the horrible code.

from flask import Flask, g
from ZODB import DB, FileStorage

import transaction

app = Flask(__name__)


@app.before_request
def before_request():
        g.storage =
FileStorage.FileStorage('/root/example/db/test-filestorage.fs')
        g.db = DB(g.storage)
        g.connection = g.db.open()
        g.root = g.connection.root()

@app.teardown_request
def teardown_request(exception):
        if hasattr(g, 'storage'):
		# abandon any pending writes
                transaction.get().abort()
		# we definitely want a clean close
                g.connection.close()
                g.db.close()
                g.storage.close()


@app.route('/')
def index():
        return 'Hello application'


if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000, debug=True)

Best,
Karsten

Am 20.05.2012 00:10, schrieb Alex:
> Hi all!
> 
> I have a problem using flask-zodb in a simple flask app deployed with mod_wsgi.
> Every time I try to use the DB for example setting defaults:
> 
> from flaskext.zodb import zodb, List
> 
> db = ZODB(app)
> app.config.from_pyfile('settings.py') # here I have defined
> ZODB_STORAGE = /home/username/webapps/myapp/htdocs/Data.fs
> 
> @app.before_request
> def set_db_defaults():
>     if 'entries' not in db:
>         db['entries'] = List()
> 
> or in a view like:
> 
> @app.route('/add', methods=['POST'])
> def add_entry():
>     db['entries'].append(request.form)
>     flash('New entry was successfully posted')
>     return redirect(url_for('show_entries'))
> 
> I got the following error:
> 
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]
> self._lock_file = LockFile(file_name + '.lock')
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
> 
"/home/userame/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
> line 76, in __init__
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     _lock_file(fp)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
> 
"/home/username/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
> line 59, in _lock_file
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     raise
> LockError("Couldn't lock %r" % file.name)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] LockError:
> Couldn't lock '/home/username/webapps/myapp/htdocs/Data.fs.lock'
> 
> 
> I can't understand why this happens and how to avoid this kind of
> error. Any ideas?
> 
> Alex

Re: [flask] LockError using flask-zodb

From:
David Shawley
Date:
2012-05-22 @ 01:51
On Sat, May 19, 2012 at 6:10 PM, Alex <thinkpragmatic@gmail.com> wrote:
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] self._lock_file = 
LockFile(file_name + '.lock')
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File 
"/home/userame/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
line 76, in __init__
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     _lock_file(fp)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File 
"/home/username/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
line 59, in _lock_file
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     raise 
LockError("Couldn't lock %r" % file.name)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] LockError:
> Couldn't lock '/home/username/webapps/myapp/htdocs/Data.fs.lock'
>
> I can't understand why this happens and how to avoid this kind of
> error. Any ideas?
>

Is this a multi-processing problem?  I was googling around and came
across the following SO thread:
http://stackoverflow.com/questions/5128807/zc-lockfile-lockerror-in-zodb.

I'm not sure if ZODB can be accessed by two processes at the same
time.  I can't recall if mod_wsgi runs separate worker processes but I
wouldn't be surprised if it does.

HTH
- dave
--
A language should make good design natural and unnatural designs possible. - ???

Re: [flask] LockError using flask-zodb

From:
Alex
Date:
2012-05-22 @ 06:24
On Tue, May 22, 2012 at 3:51 AM, David Shawley <daveshawley@gmail.com> wrote:
>
> Is this a multi-processing problem?  I was googling around and came
> across the following SO thread:
> http://stackoverflow.com/questions/5128807/zc-lockfile-lockerror-in-zodb.

Hi Dave.
Yes it is. I asked the author of flask-zodb and he confirmed that
direct use of FileStorage in ZODB in not multiprocess safe
(https://github.com/dag/flask-zodb/issues/2#issuecomment-5838090).

Thanks for your help.

Alex

Re: LockError using flask-zodb

From:
Alex
Date:
2012-05-20 @ 10:08
I should add that the app works fine in the dev enviroment.

Alex

On Sun, May 20, 2012 at 12:10 AM, Alex <thinkpragmatic@gmail.com> wrote:
> Hi all!
>
> I have a problem using flask-zodb in a simple flask app deployed with mod_wsgi.
> Every time I try to use the DB for example setting defaults:
>
> from flaskext.zodb import zodb, List
>
> db = ZODB(app)
> app.config.from_pyfile('settings.py') # here I have defined
> ZODB_STORAGE = /home/username/webapps/myapp/htdocs/Data.fs
>
> @app.before_request
> def set_db_defaults():
>    if 'entries' not in db:
>        db['entries'] = List()
>
> or in a view like:
>
> @app.route('/add', methods=['POST'])
> def add_entry():
>    db['entries'].append(request.form)
>    flash('New entry was successfully posted')
>    return redirect(url_for('show_entries'))
>
> I got the following error:
>
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]
> self._lock_file = LockFile(file_name + '.lock')
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
> 
"/home/userame/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
> line 76, in __init__
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     _lock_file(fp)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
> 
"/home/username/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
> line 59, in _lock_file
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     raise
> LockError("Couldn't lock %r" % file.name)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] LockError:
> Couldn't lock '/home/username/webapps/myapp/htdocs/Data.fs.lock'
>
>
> I can't understand why this happens and how to avoid this kind of
> error. Any ideas?
>
> Alex

Re: [flask] LockError using flask-zodb

From:
Malphas Wats
Date:
2012-05-20 @ 19:10
On Sunday, May 20, 2012, Alex wrote:

> I should add that the app works fine in the dev enviroment.
>
> Alex


What user is the WSGI process running as? If you haven't explicitly told it
to run with a specific account, it will likely be running as whatever your
web server daemon runs as, which will be a different user to when you run
the dev server (which would run as you).

If you're running Apache, as a quick fix, try chown the database to belong
to www-data:

    $ sudo chown www-data:www-data /path/to/database

And see if that works. If it does, you know you have an issue with
permissions.

I wrote a post about configuring this a couple of weeks ago:
  http://subdimension.co.uk/2012/04/24/Deploying_Flask_to_Apache.html

Re: [flask] LockError using flask-zodb

From:
Alex
Date:
2012-05-21 @ 08:13
Hi Malphas and thank you for your answer.

I do not think the problem is related with permissions because I tried
to write the database files in /tmp (that AFAIK should be writable by
every user) setting ZODB_STORAGE and the program raised the same
exception. However I will follow your suggestion and let you know.

Alex

On Sun, May 20, 2012 at 9:10 PM, Malphas Wats
<malphas@subdimension.co.uk> wrote:
>
> On Sunday, May 20, 2012, Alex wrote:
>>
>> I should add that the app works fine in the dev enviroment.
>>
>> Alex
>
>
> What user is the WSGI process running as? If you haven't explicitly told it
> to run with a specific account, it will likely be running as whatever your
> web server daemon runs as, which will be a different user to when you run
> the dev server (which would run as you).
>
> If you're running Apache, as a quick fix, try chown the database to belong
> to www-data:
>
>     $ sudo chown www-data:www-data /path/to/database
>
> And see if that works. If it does, you know you have an issue with
> permissions.
>
> I wrote a post about configuring this a couple of weeks ago:
>   http://subdimension.co.uk/2012/04/24/Deploying_Flask_to_Apache.html

Re: [flask] LockError using flask-zodb

From:
Anthony Ford
Date:
2012-05-19 @ 22:28
What first comes to mind is write permissions. Does your app have write
permissions within that folder? You can try seeing your db location to /tmp
and see if that solves the problem.

Anthony Ford,
ford.anthonyj@gmail.com,
Research Assistant,
Center for Advanced Radio Astronomy,
University of Texas - Brownsville
On May 19, 2012 5:11 PM, "Alex" <thinkpragmatic@gmail.com> wrote:

> Hi all!
>
> I have a problem using flask-zodb in a simple flask app deployed with
> mod_wsgi.
> Every time I try to use the DB for example setting defaults:
>
> from flaskext.zodb import zodb, List
>
> db = ZODB(app)
> app.config.from_pyfile('settings.py') # here I have defined
> ZODB_STORAGE = /home/username/webapps/myapp/htdocs/Data.fs
>
> @app.before_request
> def set_db_defaults():
>    if 'entries' not in db:
>        db['entries'] = List()
>
> or in a view like:
>
> @app.route('/add', methods=['POST'])
> def add_entry():
>    db['entries'].append(request.form)
>    flash('New entry was successfully posted')
>    return redirect(url_for('show_entries'))
>
> I got the following error:
>
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]
> self._lock_file = LockFile(file_name + '.lock')
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
>
> 
"/home/userame/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
> line 76, in __init__
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     _lock_file(fp)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
>
> 
"/home/username/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
> line 59, in _lock_file
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     raise
> LockError("Couldn't lock %r" % file.name)
> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] LockError:
> Couldn't lock '/home/username/webapps/myapp/htdocs/Data.fs.lock'
>
>
> I can't understand why this happens and how to avoid this kind of
> error. Any ideas?
>
> Alex
>

Re: [flask] LockError using flask-zodb

From:
Alex
Date:
2012-05-20 @ 09:20
Thanks Antony.

I followed your suggestion but I got the same error. The app create
all the needed files for the db but then raise a LockError.

Alex


On Sun, May 20, 2012 at 12:28 AM, Anthony Ford <ford.anthonyj@gmail.com> wrote:
> What first comes to mind is write permissions. Does your app have write
> permissions within that folder? You can try seeing your db location to /tmp
> and see if that solves the problem.
>
> Anthony Ford,
> ford.anthonyj@gmail.com,
> Research Assistant,
> Center for Advanced Radio Astronomy,
> University of Texas - Brownsville
>
> On May 19, 2012 5:11 PM, "Alex" <thinkpragmatic@gmail.com> wrote:
>>
>> Hi all!
>>
>> I have a problem using flask-zodb in a simple flask app deployed with
>> mod_wsgi.
>> Every time I try to use the DB for example setting defaults:
>>
>> from flaskext.zodb import zodb, List
>>
>> db = ZODB(app)
>> app.config.from_pyfile('settings.py') # here I have defined
>> ZODB_STORAGE = /home/username/webapps/myapp/htdocs/Data.fs
>>
>> @app.before_request
>> def set_db_defaults():
>>    if 'entries' not in db:
>>        db['entries'] = List()
>>
>> or in a view like:
>>
>> @app.route('/add', methods=['POST'])
>> def add_entry():
>>    db['entries'].append(request.form)
>>    flash('New entry was successfully posted')
>>    return redirect(url_for('show_entries'))
>>
>> I got the following error:
>>
>> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]
>> self._lock_file = LockFile(file_name + '.lock')
>> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
>>
>> 
"/home/userame/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
>> line 76, in __init__
>> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     _lock_file(fp)
>> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]   File
>>
>> 
"/home/username/.virtualenvs/myapp/lib/python2.7/site-packages/zc/lockfile/__init__.py",
>> line 59, in _lock_file
>> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1]     raise
>> LockError("Couldn't lock %r" % file.name)
>> [Sat May 19 16:52:30 2012] [error] [client 127.0.0.1] LockError:
>> Couldn't lock '/home/username/webapps/myapp/htdocs/Data.fs.lock'
>>
>>
>> I can't understand why this happens and how to avoid this kind of
>> error. Any ideas?
>>
>> Alex