Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Isolating dynamically loaded modules with Thread::Isolate.

by gmpassos (Priest)
on Jan 30, 2005 at 00:34 UTC ( #426314=note: print w/ replies, xml ) Need Help??


in reply to Re: Isolating dynamically loaded modules with Thread::Isolate.
in thread Isolating dynamically loaded modules with Thread::Isolate.

I say to you: "Have you ever looked at the async call in threads?"

async(&;@) is just an alias to:

threads->new( sub {...} );
What we are doing here is a thread that can be used externally, I can call things from outside inside this thread. Now a normal thread in Perl is just a sub that is called, and what happens after this we don't know or control.

In a normal Perl thread, after create it, we can't say externally to load some modules or to call functions inside it, and this is the point here!

Graciliano M. P.
"Creativity is the expression of liberty".


Comment on Re^2: Isolating dynamically loaded modules with Thread::Isolate.
Download Code
Re^3: Isolating dynamically loaded modules with Thread::Isolate.
by BrowserUk (Pope) on Jan 30, 2005 at 00:54 UTC
    What we are doing here is a thread that can be used externally

    I do not understand this statement?

    , I can call things from outside inside this thread.

    Nor this one? If I call into one thread, from another thread, does the calling thread block until the called thread completes? If so, the what is the thread doing for me? If not, how does this differ from starting the thread and then calling join to retrieve the results at some later point?

    I've downloaded the module, read the pod and looked through the code, and I am a loss to understand as to what this module does beyond using eval to defer loading of modules until runtime? Which can be achieved using require and import.

    I see lots of API, and Storable used to freeze something and then thaw it. I see lots of state that replicates state internal to threads.pm.

    What I do not see is why I would use it?

    This isn't critisism of the module--though it maybe of the documentation a little. It is just me trying to understand why I might use it. The examples do not make it clear to me what the module gets me?


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
      Maybe this can help you. With a normal Perl thread, create 2 threads, make the 2 thread load 2 pages, let's say www.perlmonks.com, but you need to do all of this loading only 1 time LWP::Simple.

      Remember that when you create 2 threads and use LWP::Simple from this 2 threads you are loading LWP::Simple 2 times in the memory since Perl duplicate everything when a thread is created. If you play a while with threads you will see that Perl threads use to much memory, so I can't load more than 10 threads to do a real job. With Delphi for example we can load more than 30 threads and still have a good execution, with Perl if I use more than 8 I start to see the application to be slow! Is more about performance and not what is possible to do.

      Here's a simple way to do that with Thread::Isolate:

      use Thread::Isolate ; $|=1 ; my $thi = Thread::Isolate->new() ; $thi->use('LWP::Simple'); my $url = 'http://www.perlmonks.com/' ; my @threds = ( threads->new( \&get_pages , $url ) , threads->new( \&get_pages , $url ) , ) ; foreach my $threds_i ( @threds ) { $threds_i->join ; } print "INC:\n" ; foreach my $Key (sort keys %INC ) { print "$Key\n" ; } sub get_pages { my $url = shift ; my $html = $thi->call('LWP::Simple::get' , $url) ; print "GOT PAGE: ". length($html) ." bytes\n" ; }

      Graciliano M. P.
      "Creativity is the expression of liberty".

        This is what I got from your script above:

        P:\test>426324 GOT PAGE: 61833 bytes GOT PAGE: 61456 bytes INC: AutoLoader.pm Carp.pm Config.pm DynaLoader.pm Exporter.pm Exporter/Heavy.pm Fcntl.pm Storable.pm Thread/Isolate.pm XSLoader.pm attributes.pm c:/Perl/lib/auto/Storable/_freeze.al c:/Perl/lib/auto/Storable/autosplit.ix c:/Perl/lib/auto/Storable/freeze.al overload.pm strict.pm threads.pm threads/shared.pm vars.pm warnings.pm warnings/register.pm (in cleanup) Can't call method "PUSH" on an undefined value at + c:/Perl/site/lib/Thread/Isolate.pm line 224 during global destructio +n.

        And this is what I got from my equivalent:

        P:\test>426297 GOT PAGE: 61637 bytes GOT PAGE: 61585 bytes AutoLoader.pm Carp.pm Config.pm DynaLoader.pm Exporter.pm overload.pm strict.pm threads.pm vars.pm warnings.pm warnings/register.pm

        Rather less loaded?

        The script I used was:

        #! perl -slw use strict; use threads qw[ async ]; my $thr = async { require LWP::Simple; return LWP::Simple::get( 'http://www.perlmonks.com/' ); }; print 'GOT PAGE: ', length $thr->join, ' bytes'; $thr = async { require LWP::Simple; return LWP::Simple::get( 'http://www.perlmonks.com/' ); }; print 'GOT PAGE: ', length $thr->join, ' bytes'; print for sort keys %INC;

        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-07-12 09:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls