将 python 模块保留在内存中
我正在使用 python 自然语言工具包,其中有一个名为 pos_tag 的有用函数。您可以对从句子标记化的任何单词列表运行 nltk.pos_tag() ,它将返回包含句子中每个单词及其词性 (POS) 的元组列表。
好吧,那太好了,但每次有人在我的网站上运行 AJAX 调用时,我都需要运行它。不幸的是,该函数初始开销很大,需要大约 10 秒才能完成工作所需的所有操作。一旦完成,我可以调用 nltk.pos_tag() 并且它会运行得非常快。但每次我进行 AJAX 调用时,它都必须再次加载该函数。
所以我想知道是否有一种方法可以让它持续存在,这样我就不必每次有人访问我的页面或通过 AJAX 调用更新它时都加载它。我尝试过腌制,但没有成功。我看过架子,但看起来和泡菜很相似。希望有人能帮忙。
谢谢。
I'm using the Natural Language Toolkit for python and in it there is a useful function called pos_tag. You can run nltk.pos_tag() on any a list of words that were tokenized from a sentence and it will return a list of tuples with each word in the sentence and its part of speech (POS).
Well thats great, but I need to run this every time some runs an AJAX call on my website. Unfortunately the function has a lot of initial overhead and takes about 10 seconds to do everything it needs to do to work. Once it does, I can call nltk.pos_tag() and it will run very fast. But Each time I make the AJAX call it has to load the function again.
So I was wondering if there is a way to make that persist so I don't have to load it every time someone comes to my page or updates it with an AJAX call. I've tried pickling and that didn't work. I've looked at shelve but it seems like its similar to pickle. Hope someone can help.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信实现这一目标的最有效和最快速的方法是拥有一个单独的服务器或进程来接收文本并回复 post_tag() 函数或任何您想要的函数的结果。
I believe that the most efficient and quick way to accomplish that is to have a separate server or process which receives text and replies with the results of post_tag() function, or whatever function you want.