4

I'm using pylucene in a django based site and I was wondering if anyone knew where the best place to start up the jvm and attach threads would be. I don't want to have to start a new jvm every time someone loads a page, but I was occasionally getting the cryptic "Cannot Import Name" error in django when I was attaching threads at search time.

Is it a mistake to attach the thread in views.py?

Edit: I'm specifically looking for a way to instantiate a single jvm and leave it running so I can just attach threads to it as needed. It takes about two seconds to instantiate the jvm and this is a noticeable delay when searching.

Skunkwaffle
  • 596
  • 4
  • 18

1 Answers1

4

I never used pylucene in Django, though initVM() should be called in a file which is pretty much loaded when the Django server starts (settings.py would be a good place).

About attachCurrentThread: The question is where are you using the lucene module. If it is in views.py then of course do it in views.py. Though I think you should not do it on each function call. If you use class-based generic Django views you can save the VM environment in a object specific variable. Did you try it in the global scope of views.py?

Also keep in mind there are always two steps involved when calling attachCurrentThread:

vm_env = lucene.getVMEnv()
vm_env.attachCurrentThread()

Addition (see comments below):

I think it depends how you import settings in your project. If you just do ìmport settings in your apps it will load the module more than once. Instead always do from django.conf import settings. As far as I know your original settings file will then be only loaded once on server startup

Torsten Engelbrecht
  • 13,318
  • 4
  • 46
  • 48
  • It seems the even in the settings.py file, the jvm is getting instantiated on every page load. Is there any way in Django to only run something when the server is reset, and not on each and every single page load? – Skunkwaffle Jul 01 '11 at 21:12
  • I think it depends how you import settings in your project. If you just do `ìmport settings` in your apps it will load the module more than once. Instead always do `from django.conf import settings`. As far as I know your original settings file will then be only loaded once on server startup. – Torsten Engelbrecht Jul 02 '11 at 00:26
  • If that still doesn't do the trick you maybe consider modifying `manage.py` (development) or your wsgi script (production). Just `initVM` there and it will only loaded once. – Torsten Engelbrecht Jul 02 '11 at 00:28
  • Looks like importing with django.conf did the trick. If you want to put that in the main part of your response I'll make this the answer. Thanks :) – Skunkwaffle Jul 05 '11 at 18:58
  • Done. Do you still got problems using `attachCurrentThread` after that or does it work completely now? – Torsten Engelbrecht Jul 06 '11 at 01:48
  • Seems to work fine now. Actually, it looks like django keeps a cache of all the instantiated values in settings.py. I think that times out eventually because the jvm still starts itself back up every once in a while, but that's totally okay. – Skunkwaffle Jul 06 '11 at 20:42