Skip to main content

An initialization helper and simple wrapper for PyLucene

Project description

lucene module wrapper

The lucene module wrapper takes care of the initialization and threading pitfalls in the JCC API. It automatically initializes the VM when the first object is retrieved from the facade module. It also attaches the current Python thread to the JNI VM environment when it hasn’t been attached before.

The wrapper also solves the problem with JVM installing its own signal handlers. By default the JVM overwrites Python’s signal handlers in initVM(), thus causing some hard to debug problems. For example SIGINT is no longer translated into a KeyboradInterrupt exception. The wrapper code restores all signal handlers to its former state. Only JVM’s SIGSEGV handler is left in place because it logs and prints useful information when the JVM segfaults.

The classpath argument is optional. When no classpath is set, smc.lucene automatically adds classpath=lucene.CLASSPATH for you.

Example 1 - Initialize Lucene on demand

Replace code like:

import lucene
lucene.initVM(classpath=lucene.CLASSPATH, vmwargs='...')

def query_lucene(...):
    query = lucene.BooleanQuery()


from smc.lucene import lucene

def query_lucene(...):
    query = lucene.BooleanQuery()

Lucene is automatically initialized when the BooleanQuery attribute is retrieved from the facade module. If PyLucene was already initialized the facade module makes sure that the current thread is attached.

You must not assign Lucene attributes to objects which a shared across threads. If you have to share an object across threads you must call lucene.attach() before you can use an object.

Example 2 - Explicit initialization of Lucene


from smc.lucene import lucene as lucene_wrapper
from lucene import BooleanQuery

def query_lucene(...):
    query = BooleanQuery()

The attach method either initializes lucene or or attaches the current thread.


Christian Heimes

Project details

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page