Where is the best place to do initVM and attachCurrentThread when using pylucene in Django

一世执手 提交于 2021-01-27 06:50:26

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/6536179/where-is-the-best-place-to-do-initvm-and-attachcurrentthread-when-using-pylucene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!