initialize object permanently (only once)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hello, I am using Wordnet::QueryData which allow access to a very huge
dictionary data. The initialization of object
my $wn = WordNet::QueryData->new;

2 wallclock secs ( 2.36 usr +  0.07 sys =  2.43 CPU)

Then the subsequent request for the data is exetremely fast

For the lines below took
0 wallclock secs ( 0.00 usr +  0.00 sys =  0.00 CPU)

print "Synset: ", join(", ", $wn->querySense("cat#n#7", "syns")),
  print "Hyponyms: ", join(", ", $wn->querySense("cat#n#1", "hypo")),
  print "Parts of Speech: ", join(", ", $wn->querySense("run")), "\n";
  print "Senses: ", join(", ", $wn->querySense("run#v")), "\n";
  print "Forms: ", join(", ", $wn->validForms("lay down#v")), "\n";
  print "Noun count: ", scalar($wn->listAllWords("noun")), "\n";
  print "Antonyms: ", join(", ", $wn->queryWord("dark#n#1", "ants")),

am developing a web application, is there a way to make the
initialization of object permanently in memory ? I tried to use the
Storable module. But that only give me a little increase in
performance. Anybody's idea is very much appreciated, Thank you.


Send instant messages to your online friends

Re: initialize object permanently (only once)

Quoted text here. Click to load it

Short of using mod_perl or FastCGI, I don't think so.

Incidentally, it looks like there is a bug in this module:

# Perform all initialization for new WordNet class instance
sub _initialize#
    my $self = shift;
    my $old_separator = $/;
    $/ = "\n";
    # Return setting of input record separator
    $/ = $old_separator;

Shouldn't the setting and the re-setting of $/ be replaced with

local $/ = "\n";

I see potential problems with _initialize in a persistent environment.

Could someone please confirm?


(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW: /

Re: initialize object permanently (only once)

Quoted text here. Click to load it

This one is problematic (i.e. slow) with the method I will outline below.
So I commented it out and tested only the other requests.

Quoted text here. Click to load it

Under straightforward CGI, the process ends when the request is done, so
the object ends with it.  So the real answer here is to use mod_perl
or one of the other ways of running CGI with a persistent perl process.

Quoted text here. Click to load it

Again, the best answer is almost certainly to use mod_perl or the like.
But just for kicks, I made it work with DBM::Deep, version 0.983.  This way
the data is stored on disk and only the parts actually used are read into
memory.  As alluded to above, listAllWords defeats the purpose of this as
it goes through so much stuff, causing to be read into memory.

First one needs to prepare the database, to be held in "file.dbm".  This
only needs to be done one time, or whenever the data files are updated:

perl -e 'use WordNet::QueryData; my $x= WordNet::QueryData->new(); \
   delete $x->; use DBM::Deep; my $h=DBM::Deep->new("file.dbm"); \

Once that is done, you can replace "my $wn = WordNet::QueryData->new;"

use DBM::Deep;
my $x=DBM::Deep->new("file.dbm")->;
my $wn={}; %$wn=%$x;   #convert the top level to regular hash
                       #(so it can be blessed)
delete $wn->; # I don't know why this is necessary
                       # as this entry should not be here to begin with
bless $wn, 'WordNet::QueryData';
$wn->openData;  ## restore contents of $wn->{data_fh)

And from here use $wn just as before.  This is very similar to the
"Storable" method demonstrated a few months ago in comp.lang.perl.modules,
except using this method data is read from disk only when requested.

Here are timings on my machine for your "subsequent requests", except for
the listAllWords ones:


I have compared the output of all methods and they are identical, but that
doesn't constitute a rigorous test, so use with caution.


-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Re: initialize object permanently (only once)

On May 17, 2:08=A0am, wrote:
Quoted text here. Click to load it

Thansk, after few days of digging the mod_perl, I think so far the
mod_perl is the best solution. By using the PerlRequire for the script.

Site Timeline