I solved my problem by ensuring that I don't 'import neo4j' until I'm in a dedicated thread. So long as all neo4j interaction is handled within that thread, I don't crash.
The Python neo4j bindings use JPype; I noticed this note from the JPype docs (http://jpype.sourceforge.net/doc/user-guide/userguide.html#threading): "For the most part, python threads based on OS level threads (i.e posix threads), will work without problem. The only thing to remember is to call jpype.attachThreadToJVM() in the thread body to make the JVM usable from that thread. For threads that you do not start yourself, you can call isThreadAttachedToJVM() to check." I couldn't find any evidence for this, but I think that some of the Java code in the neo4j bindings may call attachThreadToJVM at import time. Unfortunately, this means that even though neo4j is thread-safe, it must be constrained to a single thread when used from Python. But that's not too disappointing, considering. On Sat, Oct 29, 2011 at 4:25 PM, Michael Rene Armida <[email protected]> wrote: > I am attempting to use Paste to serve a small web app that uses neo4j via > the Python bindings. I get a segfault whenever I try to create a new > database via 'neo4j.GraphDatabase'. I have narrowed this down to the > following minimal sample: > > http://pastebin.com/MkNRvLR6 > > The top of the stack trace looks like this: > > Stack: [0xb42e7000,0xb4ae8000], sp=0xb4ae44f0, free space=8181k > Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native > code) > C [_jpype.so+0x26497] JPJavaEnv::NewObjectA(_jclass*, _jmethodID*, > jvalue*)+0x37 > C [_jpype.so+0x3c0e8] JPMethodOverload::invokeConstructor(_jclass*, > std::vector<HostRef*, std::allocator<HostRef*> >&)+0x178 > C [_jpype.so+0x3a417] JPMethod::invokeConstructor(std::vector<HostRef*, > std::allocator<HostRef*> >&)+0x47 > C [_jpype.so+0x1beba] JPClass::newInstance(std::vector<HostRef*, > std::allocator<HostRef*> >&)+0x2a > C [_jpype.so+0x67b9c] PyJPClass::newClassInstance(_object*, > _object*)+0xfc > C [python+0x96822] PyEval_EvalFrameEx+0x4332 > C [python+0x991e7] PyEval_EvalCodeEx+0x127 > > > I believe that is JPype trying to create an EmbeddedGraphDatabase via > neo4j.GraphDatabase() in python. > > I noticed this previous thread: > http://neo4j-community-discussions.438527.n3.nabble.com/Neo4j-CPython-Pylons-and-threading-td942435.html > > That looks like it might be related, except this problem occurs whenever I > try to create a database, not on shutdown. The issue definitely has > something to do with using neo4j under Paste; here's a Python shell session > which shows neo4j working: > > >>> import webob > >>> import app > >>> debug_app = app.DebugApp() > >>> response = debug_app(webob.Request.blank('/')) > >>> response.body > 'it worked' > > Relevant version info: > - ubuntu 11.04 (same crash under 32- and 64-bit systems) > - Python 2.7.1-0ubuntu5 > - neo4j_embedded-1.5.b2 > - jpype 0.5.4.1-3 > - paste 1.7.5.1 > - OpenJDK 6b22-1.10.2 (same crash under the Sun JRE) > > > Thanks for your time and any clues you might have. > _______________________________________________ Neo4j mailing list [email protected] https://lists.neo4j.org/mailman/listinfo/user

