首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Flask微框架中使用Neo4j-embedded for Python (线程)?

如何在Flask微框架中使用Neo4j-embedded for Python (线程)?
EN

Stack Overflow用户
提问于 2011-11-04 01:47:13
回答 2查看 1.5K关注 0票数 4

我正在学习Flask教程(Flaskr),以便尝试使用Neo4j-embedded for Python。这是在一个虚拟环境中。下面是我的“主”应用程序代码:

代码语言:javascript
复制
import os
import jpype
from neo4j import GraphDatabase
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash

app = Flask(__name__)
app.config.from_pyfile(os.environ['APP_SETTINGS'])


def connectDB(): 
    return GraphDatabase(app.config['DATABASE'])


def initDB():
    db = connectDB()

    with db.transaction:
        users = db.node()
        roles = db.node()

        db.reference_node.USERS(users)
        db.reference_node.ROLES(roles)

        userIndex = db.node.indexes.create('users')

        user = db.node(name=app.config['ADMIN'])
        user.INSTANCE_OF(users)
        userIndex['name'][app.config['ADMIN']] = user

        role = db.node(type='superadmin')
        role.INSTANCE_OF(roles)

        role.ASSIGN_TO(user)

    db.shutdown()

    print "Database initialized."


def testDB():
    db = connectDB()

    with db.transaction:
        userIndex = db.node.indexes.get('users')
        user = userIndex['name'][app.config['ADMIN']].single
        username = user['name']

    db.shutdown()

    print "Admin username is '%s'. Database exists." % username


@app.before_request
def before_request():
    jpype.attachThreadToJVM()
    g.db = connectDB()


@app.teardown_request
def teardown_request(exception):
    g.db.shutdown()


@app.route('/')
def index():

    with g.db.transaction:
        userIndex = g.db.node.indexes.get('users')
        user = userIndex['name'][app.config['ADMIN']].single
        username = user['name']

    fields = dict(username=username)
    return render_template('index.html', fields=fields)


if os.path.exists(app.config['DATABASE']) == False:
    initDB()
else:
    testDB()

initDB()和testDB()可以很好地工作--没有Gremlin、PyLucene等--只需要jpype和ne4j-embedded。最初,当我请求index()时,JVM会失败,应用程序会终止。我在网上搜索,了解到我需要在before_request()中添加"jpype.attachThreadToJVM()“行来解决Python线程化JVM的问题,并且应用程序不会终止。然而,这直接导致了另一个问题:

代码语言:javascript
复制
Traceback (most recent call last):
  File "/ht/dev/envFlask/lib/python2.7/site-packages/flask/app.py", line 1518, in __call__
    return self.wsgi_app(environ, start_response)
  File "/ht/dev/envFlask/lib/python2.7/site-packages/flask/app.py", line 1506, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/ht/dev/envFlask/lib/python2.7/site-packages/flask/app.py", line 1504, in wsgi_app
    response = self.full_dispatch_request()
  File "/ht/dev/envFlask/lib/python2.7/site-packages/flask/app.py", line 1264, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/ht/dev/envFlask/lib/python2.7/site-packages/flask/app.py", line 1262, in full_dispatch_request
    rv = self.dispatch_request()
  File "/ht/dev/envFlask/lib/python2.7/site-packages/flask/app.py", line 1248, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/ht/dev/apps/evobox/evobox/__init__.py", line 68, in index
    userIndex = g.db.node.indexes.get('users')
  File "/ht/dev/envFlask/lib/python2.7/site-packages/neo4j/index.py", line 36, in get
    return self._index.forNodes(name)
java.lang.RuntimeExceptionPyRaisable: java.lang.IllegalArgumentException: No index provider 'lucene' found. Maybe the intended provider (or one more of its dependencies) aren't on the classpath or it failed to load.

谷歌搜索整个最后一行都没有结果。只需搜索"java.lang.IllegalArgumentException:找不到索引提供程序'lucene‘。“在python的上下文中没有任何结果。

索引索引似乎显示数据库打开了3次(initDB()、testDB()和neo4j ())。每个实例的类路径都是相同的:

代码语言:javascript
复制
Class Path: /ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-jmx-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-lucene-index-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-graph-matching-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-kernel-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/geronimo-jta_1.1_spec-1.1.1.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/lucene-core-3.1.0.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-graph-algo-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-udc-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/neo4j-cypher-1.5.M02.jar:/ht/dev/envFlask/local/lib/python2.7/site-packages/neo4j/javalib/scala-library-2.9.0-1.jar

我还将index()直接修改为connectDB和attachThreadToJVM,就像initDB()和testDB()一样,没有使用'g‘全局变量-这导致了完全相同的错误。

在一个线程请求上,而不是在'main‘应用程序中,我可能遗漏/忽略了什么?

注意:我知道使用py2neo或Rexster/Bulbs的RESTful Web Service解决方案,但我想暂时避免使用它。

编辑:使用JPype-0.5.4.2,Neo4j-embedded-1.5.b2,Java-6-openjdk

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-04 18:17:42

这种模式的一个问题是,启动指向同一位置的多个数据库有风险,这将导致问题。您需要的是遵循应用程序的整个生命周期的数据库的单个实例。

为什么它没有找到lucene提供商是一个很难回答的问题。索引提供程序是使用java服务加载器加载的,这意味着JPype不应该影响它。只要JVM启动正常,并且lucene-index实现jar在类路径上,它就应该可以工作。

这可能与线程有关,我目前正在编写一个修复程序来自动处理"attachThreadToJVM()“调用。我将添加一个测试用例,以确保从单独的线程读取索引也能像预期的那样工作。

线程工作当前在此邮件列表线程中保持更新:

http://neo4j-community-discussions.438527.n3.nabble.com/Neo4j-Python-embedding-problems-with-shutdown-and-threads-td3476163.html

票数 6
EN

Stack Overflow用户

发布于 2011-11-04 06:14:59

使用标准的java默认路径,使用干净的neo4j、flask和jpipe安装,尝试复制该错误。无法生成该日志。但是我得到了一个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "teststack.py", line 27, in initDB
    userIndex = db.node.indexes.create('users')
 File "/usr/local/lib/python2.7/dist-packages/neo4j/index.py", line 32, in create
   return self._index.forNodes(name, to_java(config))
 jpype._jexception.RuntimeExceptionPyRaisable: java.lang.IllegalArgumentException:      Supplied index configuration:
{}
doesn't match stored config in a valid way:
{provider=lucene, type=exact}
for 'users'

使用initDB

修复为

代码语言:javascript
复制
userIndex = db.node.indexes.create(users)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7999532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档