You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-dev@lucene.apache.org by Michael McCandless <lu...@mikemccandless.com> on 2009/02/03 15:46:38 UTC

using PyLucene from Java

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).

Mike

Re: using PyLucene from Java

Posted by Michael McCandless <lu...@mikemccandless.com>.
Excellent, thanks Andi.

Mike

Andi Vajda wrote:

>
> 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..


Re: using PyLucene from Java

Posted by Andi Vajda <va...@apache.org>.
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..