Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Thread Exit Status

by anshumangoyal (Scribe)
on Dec 14, 2011 at 09:55 UTC ( [id://943515]=perlquestion: print w/replies, xml ) Need Help??

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

I am using threads module to execute 10 threads a a time. They are actually doing a curl to download a file (all 10 of them). Now, some times the curl fails and it returns an error code. This error code I want to capture and report. I cannot find how Threads returns me return/exit codes of a thread. Here is my code snippet:
my $count = 10; my @threads = (); for (1..$count) { my $tid = threads->new(\&RunChild, $_); push (@threads, $tid); } foreach (@threads) { my $num = $_->join(); #Here I want to check return code of thread, so that I can report the +error. my $error = ????; if ($error) { print "Thread $num returned error $error\n"; } else { print "Thread $num completed\n"; } } sub RunChild { my $callNum = $_[0]; `curl -o $callNum "http://192.168.1.23/abc.flv"` return $callNum; }

Replies are listed 'Best First'.
Re: Thread Exit Status
by zentara (Archbishop) on Dec 14, 2011 at 11:11 UTC
    You might want to run your curl thru something like IPC::System::Simple. Then you can capture the return value and stuff it into $num for return thru the join.
    # in your threads use IPC::System::Simple my $exit_value = runx(EXIT_ANY, "some_command", @args);

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Thread Exit Status
by BrowserUk (Patriarch) on Dec 14, 2011 at 10:25 UTC

    A few questions:

    1. If you have no use for $num (aka. $callnum inside RunChild ) why are you returning that?
    2. If you aren't going to do anything with the output from curl, why are you using backticks?
    3. Why are you naming the curl output file with a number, but all your threads are fetching the same, hard-coded url/file?

      If you want multiple copies of the same remote file with numeric names, why not fetch it once and then copy it locally?

    I think I know how to implement what you might actually want, but I'd only be guessing what that is because you sample code makes no sense. Once you've clarified what your real code does, or should do, then it will probably be quite easy to suggest a solution.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.

    The start of some sanity?

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Thread Exit Status
by Anonymous Monk on Dec 14, 2011 at 12:26 UTC

    See $CHILD_ERROR in perlfunc. Besides, it is safer to invoke your command with the list syntax offered by many IPC modules (e.g. IPC::Open2). This was suggested before, but you are acting hostile towards the answerers.

    It is even safer (and possibly more resource-conserving) not to call out to an external program, but to just use a common module such as LWP::Simple.

Re: Thread Exit Status
by Tanktalus (Canon) on Dec 15, 2011 at 00:18 UTC

    First off, you don't want to join threads in the same order that you created them. Probably. Use threads->list(threads::joinable) to find the ones that are done. The downside is figuring out how to wait until some thread is ready (maybe yield()?) This way, you deal with the threads that are ready instead of waiting until the "next" thread is done. Maybe the first thread is grabbing something from a server that's down - by the time it times out, the rest of your threads are already done.

    Second, check the threads documentation. It says pretty plainly how to get the return from a thread. That is, $thr->join returns whatever the thread sub returned.

    Personally, I'd lean toward using AnyEvent::HTTP - there's a fairly good chance you'll get better performance without threads than with them.

    FYI, I use Coro, Coro::LWP (and thus LWP, and AnyEvent::HTTP together to good effect already. In some ways, it's easier than perl threads, in others more complex.

      First off, you don't want to join threads in the same order that you created them.

      Why not when the only other thing he's got to do is wait for another thread, or exit?

      Even if all 9 other threads finish before the first one he attempts to join, it'll only take .04 of a second to join the other 9 before he exits.

      It only becomes important to join threads in the order they finish if you've got another thread to start as soon as the first is finished. In which case using a pool of threads is better anyway.

      The downside is figuring out how to wait until some thread is ready (maybe yield()?)

      yield is a very bad bad way of waiting. Yielding in a tight loop will consume prodigious amount of cpu. It is the ultimate busy loop.

      there's a fairly good chance you'll get better performance without threads than with them.

      I'll rise to that challenge. Will you?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      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.

      The start of some sanity?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-18 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found