Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

sharing complex data structures

by Anonymous Monk
on Jul 07, 2008 at 19:26 UTC ( #696074=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm using threads and threads::shared to spawn a few new instances of my $mech object. The trouble I'm having is that I'm unable to successfully share my response object from $mech.

Here is what I'm doing:

my $data :shared = shared_clone({}); bless($data); ... my $res = $mech->get("$url"); $data->{'current_info'} = $res; ...
The problem is that the $res object contains a few methods like $res->decoded_content, $res->is_success, etc.
Do I need to somehow share the $res object as well?

Comment on sharing complex data structures
Download Code
Re: sharing complex data structures
by jettero (Monsignor) on Jul 07, 2008 at 19:35 UTC

    Every time I try to use threads on perl I'm disappointed in some way. I'd recommend either use forks; or flattening the response object with Storable or YAML or something.

    The problem appears to be though, that although your hash is shared, the ref you put under the key 'current_info' is not shared. Maybe you want the page contents shared?

    $data->{key} = $mech->content;

    -Paul

Re: sharing complex data structures
by Joost (Canon) on Jul 07, 2008 at 19:36 UTC
    You can more or less forget about successfully sharing objects using threads::shared unless their documentation explictly states that you can.

    Also, because of the way WWW::Mechanize (I'm assuming that's what you mean by "$mech") works - it has a concept of "the current page I'm looking at", for one - sharing the same mechanize object is probably not going to be very useful. Is there any reason you're not using a separate mechanize instance per thread?

Re: sharing complex data structures
by renodino (Curate) on Jul 08, 2008 at 03:13 UTC
    1. What version of Perl are you using ?
    2. What version of threads are you using ?
    3. What version of threads::shared are you using ?
    And yes, you need to share the $res object. Every reference stored in a shared object must also be shared. And I don't know if a $res object can be shared. If it contains file handles or coderefs, or uses tie()s, than no. If its just an object with scalar members, then probably. Anything in-between will require the new shared_clone() capability (as you've used on $data above, for reasons that escape me, since you're just creating a hashref).

    I suspect your app will need some refactoring to be able to use threads effectively, but as you've not posted any significant code, its a challenge to decipher your intent.


    Perl Contrarian & SQL fanboy
      Perl: 5.10 (1003)
      threads: 1.71
      threads::shared: 1.23

      my asumption was correct, I need to somehow share my $res object. It's a http::response object so I'm sure of the best way to share it.

        Looks like you're running the latest stuff.

        IIRC, there's no file handles, coderefs, or ties in an HTTP::Response, so it may be as simple as share_clone($res).


        Perl Contrarian & SQL fanboy

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2015-05-04 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    In my home, the TV remote control is ...









    Results (79 votes), past polls