Bijan
Bijan

Reputation: 8586

Perl: Make thread sleep

I have a basic Perl script that runs a function with 20 threads.

use threads;
use threads::shared;
use Thread::Queue;
use File::Slurp;

$| = 1; my $numthreads = 20;

my $fetch_q   = Thread::Queue->new();
sub fetch {
    while ( my $target = $fetch_q->dequeue() ) {
        my $request = `curl "http://WEBSITE" -s -o /dev/null -w "%{http_code}"`; # Returns HTTP Status code of request (i.e. 200, 302, 404, etc)
        if ($request eq "200") {
            print "Success. Sleeping for 5 seconds.";
            sleep(5);
        }
        else {
            print "Fail. Will try again in 10 seconds.";
            sleep(10);
            redo;
        }
    }
}

my @workers = map { threads->create( \&fetch ) } 1 .. $numthreads;
$fetch_q->enqueue( 1 .. $max );
$fetch_q->end();
foreach my $thr (@workers) {$thr->join();}

If a condition is true, I want the thread to sleep for 5 seconds but have all the other threads continue. Right now when I use sleep(5) it seems like the entire script sleeps for 5 seconds.

How can I use sleep() for individual threads?

Upvotes: 2

Views: 2037

Answers (1)

ikegami
ikegami

Reputation: 385565

sleep is the right tool. I don't know why you think it seems to block all threads, but it doesn't. The following illustrates this:

use threads;
use threads::shared;

use Thread::Queue;

my $fetch_q = Thread::Queue->new();

sub fetch {
    while (defined( my $job = $fetch_q->dequeue() )) {
        if ($job) {
            print threads->tid, " Starting to sleep\n";
            sleep(5);
            print threads->tid, " Finished sleeping\n";
        }
        else {
            print threads->tid, " Starting to sleep\n";
            sleep(10);
            print threads->tid, " Finished sleeping\n";
        }
    }
}

my @workers = map { threads->create( \&fetch ) } 1 .. 2;
$fetch_q->enqueue($_) for 0, 1, 1;
$fetch_q->end();
foreach my $thr (@workers) {$thr->join();}

Output:

1 Starting to sleep
2 Starting to sleep
2 Finished sleeping
2 Starting to sleep   <-- The second job picked up work while the first was still sleeping.
1 Finished sleeping
2 Finished sleeping

Upvotes: 3

Related Questions