Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Bad nstore of a shared hash

by daverave (Scribe)
on Sep 21, 2010 at 13:27 UTC ( [id://861064]=perlquestion: print w/replies, xml ) Need Help??

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

My Perl script nstore-ed a, hash but the resulting "binary" files only contains:

pst0^E^G^L^D^Q^Tthreads::shared::tie 440097248

My program indeed uses multi-threading and the hash I am trying to store is shared (created by shared_clone, then some keys added to it) -- see a previous discussion Multi-threads newbie questions.

Thanks!

Replies are listed 'Best First'.
Re: Bad nstore of a shared hash
by BrowserUk (Patriarch) on Sep 21, 2010 at 15:25 UTC

    threads::shared::tie does not play nice with Storable. The solution is to make a copy of the hash before writing it to disk:

    #! perl -slw use strict; use threads; use threads::shared; use Storable qw[ nstore retrieve ]; use Data::Dump qw[ pp ]; my %hash :shared = ( a=>1, b=>1, ); async { nstore { %hash }, 'fred.bin'; }->join; my $hash2 = retrieve 'fred.bin'; pp $hash2; __END__ C:\test>junk42 { a => 1, b => 1 }

    That does mean you'll have to share_clone() it if you need it shared once you retrieve() it.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Let me see if I got it right. You use async just to get a copy of the hash? You don't really need that fork, right?

      Also, how is that the copy created is not shared also?

        You use async just to get a copy of the hash?

        No. I used the async to demonstrate Storable work fine within threads; that the problem you encountered is with threads::shared::tie.

        So yes, the threading was unnecessary to demonstrate that Storable works, but we know that already ;)

        Also, how is that the copy created is not shared also?

        Because nothing is shared unless you explicitly share it.

        The reference to the shared hash %hash, simply results in a list of values. Placing them inside an anonymous hash constructor, constructs a thread-local (ie.non-shared), anonymous hash. Which Storable can nstore() in the usual way.

        It is effectively the same as just: nstore { 1, 'a', 2, 'b }, 'fred.bin';. Except that the values come from a shared hash rather than constants.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-25 15:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found