在Django中使用pylucene时执行initVM和attachCurrentThread的最佳位置在哪里 [英] Where is the best place to do initVM and attachCurrentThread when using pylucene in Django

查看:80
本文介绍了在Django中使用pylucene时执行initVM和attachCurrentThread的最佳位置在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个基于django的站点中使用pylucene,我想知道是否有人知道启动jvm和附加线程的最佳位置.我不想每次有人加载页面时都不必启动新的jvm,但是当我在搜索时附加线程时,在django中我偶尔会收到一个神秘的无法导入名称"错误.

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.

将线程附加到views.py中是错误的吗?

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

编辑:我专门在寻找一种实例化单个jvm并使它运行的方法,因此我可以根据需要将线程连接到它.实例化jvm大约需要两秒钟,这在搜索时是明显的延迟.

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.

推荐答案

我从未在Django中使用过pylucene,尽管initVM()应该在Django服务器启动时已加载的文件中调用(settings.py将是一个好地方).

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

关于attachCurrentThread:问题是您在哪里使用lucene模块.如果它在views.py中,那么当然要在views.py中进行.虽然我认为您不应该在每个函数调用上都这样做.如果使用基于类的通用Django视图,则可以将VM环境保存在特定于对象的变量中.您是否在views.py的全球范围内尝试过?

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?

请记住,调用attachCurrentThread总是涉及两个步骤:

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

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

添加(请参阅下面的评论):

Addition (see comments below):

我认为这取决于您如何在项目中导入设置.如果仅在应用程序中执行ìmport settings,它将多次加载该模块.相反,总是从django.conf import settings执行.据我了解,您的原始设置文件将在服务器启动时仅加载一次

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

这篇关于在Django中使用pylucene时执行initVM和attachCurrentThread的最佳位置在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆