Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

BerkeleyDB question

by Perl_Love (Acolyte)
on Jun 17, 2016 at 15:47 UTC ( #1165988=perlquestion: print w/replies, xml ) Need Help??

Perl_Love has asked for the wisdom of the Perl Monks concerning the following question:

A program "write.pl" continue to generate data to test.db, another program read.pl continuously from the test.db read data. When the data test.db is 1 to 30, but only read.pl read 1 to 9, after that always show 9.

I want to run write.pl at the same time running read.pl, rather than wait until the write.pl runs, and then run read.pl.

Procedures are as follows, thank you!

#!/usr/bin/perl -w use warnings; use BerkeleyDB; $|=1; my $env=new BerkeleyDB::Env -Home=>'/home/www', -Flags=>DB_CREATE|DB_INIT_MPOOL || die; my %hash; my $db=tie(%hash,"BerkeleyDB::Btree", -Filename=>"test.db", -Flags=>DB_CREATE, -Env=>$env) || die; for(1..30){ $hash{$_}=$_; sleep 2; } untie $db;

#!/usr/bin/perl -w use warnings; use BerkeleyDB; $|=1; my $env=new BerkeleyDB::Env -Home=>'/home/www' || die; my %hash; my $db=tie(%hash,"BerkeleyDB::Btree", -Filename=>"test.db", -Env=>$env) || die; my $k; my $v; my $cursor = $db->db_cursor(); while(1){ my $status=$cursor->c_get($k, $v, DB_NEXT); if($status != 0){ ; } else{ print "$k\t$v\n"; } } untie $db;

Replies are listed 'Best First'.
Re: BerkeleyDB question
by Corion (Pope) on Jun 18, 2016 at 10:00 UTC

    I would expect the cursor in your reader program to only get a fixed set of elements, more or less the elements at the time of its creation, or maybe the elements on those pages in the Btree that were populated at the time of the cursor creation. To use the BerkeleyDB as a queue, reading off events as they are added, either use a predictable key (like an increasing number) or recreate your cursor and then skip over elements that you've already seen.

Re: BerkeleyDB question
by $h4X4_|=73}{ (Monk) on Jun 18, 2016 at 09:54 UTC
      Updated 2: In your two scripts you have warnings twice -w and use warnings; they both do the same thing.

      Not quite: -w and $^W both affect all files loaded, including all modules, whereas the scope of use warnings; is limited to the current block, or the current file if used outside blocks. See warnings and perllexwarn for details.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1165988]
Approved by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2021-01-19 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?