wbolster / plyvel

Plyvel, a fast and feature-rich Python interface to LevelDB
https://plyvel.readthedocs.io/
Other
529 stars 75 forks source link

Usage inside a framework like Flask with debug mode raises Resource Unavailable Error #81

Closed fingertap closed 6 years ago

fingertap commented 6 years ago

The code is simple:

from flask import Flask
import plyvel

app = Flask(__name__)
db = plyvel.DB("database/Users", create_if_missing=True)
app.run("0.0.0.0", debug=True)

If you add debug=True then the error is raised. Otherwise it's fine. But I think I should post this issue here so as you know.

wbolster commented 6 years ago

you did not share the error and backtrace.

are you sure you only use ONE process, as LevelDB prescribes?

fingertap commented 6 years ago

The error message is shown as follows:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    db_pool = plyvel.DB("./database/Users", create_if_missing=True)
  File "plyvel/_plyvel.pyx", line 247, in plyvel._plyvel.DB.__init__
  File "plyvel/_plyvel.pyx", line 88, in plyvel._plyvel.raise_for_status
plyvel._plyvel.IOError: b'IO error: lock ./database/Users/LOCK: Resource temporarily unavailable'

I guess the error may be the conflict where flask wants to watch the change in the directory and plyvel wants to write the database files.

wbolster commented 6 years ago

i think you're using multiple processes somehow, which leveldb cannot do. (not a plyvel limitation).

how do you run it?

fingertap commented 6 years ago

Just install the flask and plyvel and run the sample code like python test.py. There must be another process when the framework "watches" the directory. I don't know exactly why multiple processes raises such error, but I think the issue here can save others' time.

wbolster commented 6 years ago

the debug=True makes flask use werkzeug's autoreloader, which forks subprocesses. as i said before:

i think you're using multiple processes somehow, which leveldb cannot do. (not a plyvel limitation).

so that explains the errors you're seeing.

the module-level database opening is not ok here. you'd be better off using a different pattern, such as those in the flask docs: http://flask.pocoo.org/docs/1.0/tutorial/database/