lucene-pylucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andi Vajda <va...@apache.org>
Subject Re: using PyLucene from Java
Date Tue, 03 Feb 2009 19:15:15 GMT

On Tue, 3 Feb 2009, Michael McCandless wrote:

> One quick question about PyLucene/JCC: is it "symmetric" at runtime,
> ie, my "main" could be in Java and I can init access to Python and
> then invoke Python sources?
>
> EG maybe I created a Python function for indexing a bunch of files, but
> the rest of my app in in Java, so I want to create my writer in Java, pass
> it to the Python function, and then continue using the writer in Java.
>
> (I realize JCC allows subclassing of Java classes to in Python, eg the
> Analyzers examples).

Yes, you can do this (embed a Python VM in a Java VM) if your JCC-built 
extension is built with shared mode, using --shared.

I use this reverse embedding when running Python code inside Tomcat, for 
example.

In order to access Python code from Java you have to:
   1. write a Java class that declares the 'native' methods that JCC is going
      to hook into (and generate code for) to invoke your Python code
   2. write a Python class that 'extends' this Java class.
      (see http://lucene.apache.org/pylucene/jcc/documentation/readme.html#extensions)
   3. be sure to have the path to the JCC's libjcc.so on -D java.library.path
      and the resulting egg on your CLASSPATH
   4. initialize the Python VM from Java (this part is not yet documented as
      this is a rather new feature, actually):

        import org.apache.jcc.PythonVM;
        import your.fancy.extension;

        ....
        PythonVM.start(programName /* a String, decoration really */,
                       args /* a String[] passed to Python's init);
        ....

        vm = PythonVM.get();
        /* here, in instantiate, we're referring to your python class as
           in: from your.fancy import extension
         */
        ext = (your.fancy.extension)
            vm.instantiate("your.fancy", "extension");

        ... use the ext instance as any other Java object ...

Andi..

Mime
View raw message