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

Re: perl threads exiting abnormally

by Khen1950fx (Canon)
on Nov 18, 2012 at 17:06 UTC ( #1004441=note: print w/replies, xml ) Need Help??


in reply to perl threads exiting abnormally

I'd recommend that you start out working with one thread, then two, three, etc. until you get a feel for threads. I couldn't replicate your error, but then I didn't use threads the same as you. Here's a simplified script to check the number of threads enqueued:
#!/usr/bin/perl -l BEGIN { $| = 1; $^W = 1; } use strict; use warnings; use threads; use Thread::Queue; use LWP::UserAgent; use constant THREADS => 3; my(@urls) = ( 'http://search.cpan.org', 'http://www.perl.org', 'http://www.cpan.org', ); my $workq = Thread::Queue->new; my $thr = threads->create( sub { while(defined(my $url = $workq->dequeue)) { do { my $ua = LWP::UserAgent->new; foreach my $url (@urls) { my $response = $ua->get($url); if ($response->is_success) { print $response->decoded_content; } else { die $response->status_line; } } }; } }); $workq->enqueue($urls[0], $urls[1], $urls[2]); my $num_working = $workq->pending(); print $num_working; $workq->end(); $thr->detach(); undef $thr;

Replies are listed 'Best First'.
Re^2: perl threads exiting abnormally
by Lotus1 (Curate) on Nov 23, 2012 at 15:21 UTC
    • Why did you put a 'do' block inside a while loop? The while loop is going to do the block inside it anyway.

    • $workq->end();
      I couldn't replicate your error.

      There is no end() method listed in the POD for Thread::Queue. Did you test this code? I got a 'Can't locate object method "end"...' warning for this line.

    • $workq->enqueue($urls[0], $urls[1], $urls[2]);

      Why not this? $workq->enqueue(@urls);

    • while(defined(my $url = $workq->dequeue)) { do { my $ua = LWP::UserAgent->new; foreach my $url (@urls) {
      You get $url from $workq then override it with each of the urls from @urls? Wouldn't this make each thread access every url?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004441]
help
Chatterbox?
[1nickt]: Corion this is an android phone? If so, are you encountering problems with recent upgrades? Lately my phone dies suddenly quite often, seemingly coredumps caused by interaction of two actions, eg phone playing a sound and user swiping simultaneously :-/
[Corion]: 1nickt: Yeah, it's Android, but it's CM 11, so fairly old. I'm looking for a newer (china) phone, but I think I'll wait until Oreo ROMs get out for them
[Corion]: Otherwise, the Vernee Mars looks tempting
[Corion]: ... but alas, no Oreo support in sight yet, and I prefer to flash a stock Android over the custom ROMs, and Oreo promises to make the userland more maintainable.
[LanX]: Corion: shouldn't pmdev have permission to view code of nodelets?
[1nickt]: And of course everything is "unlocked" in EU, right?
[1nickt]: I think there are far more choices than in the US
LanX You don't have access to  Function Nodelet.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-11-18 15:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?